[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] "," instead of ";" in "for"



Thanks for your responses. I get the idea of evaluating the expressions in
the list once. I'm curious as to whether the iterations after that is from
a simple count of list elements, or whether it iterates back through the
list one by one.

Anyway, I did some testing [got a bit long winded] but any further comment
would be sincerely appreciated.

for (X){
 print 'looped a time',"\n";
}
OUTPUT:
Y

All tested with $^W false:

Test	X		Y
1	3		1 loop
2	3;		# syntax error, near ";)"Execution aborted
3	3,		1 loop
4	3;5		# syntax error, near "5)"Execution aborted
5	3;5;		Runaway loop
6	3,5		2 loops
7	3,5;		# syntax error, near ";)"Execution aborted
8	3;5,		# syntax error, near ",)"Execution aborted
9	3,5,		2 loops
10	3;5,7		# syntax error, near "7)"Execution aborted
11	3,5;7		# syntax error, near "7)"Execution aborted
12	3,5,7		3 loops
13	3,5,7,$i=1,9,12	6 loops
14	3,5,7,$i=1;9,12	# syntax error, near "12)"Execution aborted
15	3;5;7		Runaway loop
16	3;5;7;		# syntax error, near "7;"Execution aborted
17	3;5;7;8		# syntax error, near "7;"Execution aborted
18	3;5;7,8		Runaway loop
19	3;5,7;8		Runaway loop
20	3;5;7;8;3	# syntax error, near "7;"Execution aborted
21	$i=0;$i++;	No visible output at all [errors or other]
22	9 10		# syntax error, near "9 10"Execution aborted
23	$i=0;$i<10; $i++,$i++	5 loops
24	4;5,6;7,9	Runaway loop
25	4,5,0,7,9	5 loops
26	4,5,,7,9	4 loops
27	$i=0;$i<5,$x=1;$i++	Runaway loop
28	$i=0;$x=1,$i<5;$i++	5 loops

It seems like the interpretor, if it sees a ";" inside "()" after "for" it
needs to see exactly two to be happy. Now I know that "," is the list
seperator, and I know that ";" is the statement terminator.

I check:
#! perl
4,6,8,27;

It runs without errors, though producing no visible output. I come to the
conclusion that ";" or "," don't have any special interpretation inside
"()" following "for". But if ";" present, there must be exactly two, and
location of statement within "()" following "for" relate to when
tested/evaluated/re-evaluated.

I'm thinking maybe the behaviour can be explained by thinking about list
and scalar context, that when "for" sees only a simple list it evaluates
all expressions, then iterates through each one then executes the block. I
wonder whether the number of loops is simply a count of list elements or a
true false test of each element. I do 25 and 26.

So my conclusion is that the for statement either takes a list of
expressions, or 3 statements.
* That if the statements option is chosen there must be 3 statements which
can be empty [not great word].
* That the location of the statement determines if it is evaluated once,
checked for truth, or evaluated each loop.
* That if the statements option is chosen, and a statement is a list of
expressions, that the last expression is the one used to play it's role
determined by the statements location.
* That if the list of expressions as one statement option is chosen, that
all expressions in the list are evaluated once at beginning. Then the block
is executed the same number of times as the total elements in the list.
That is, it's a count thing rather than iterating back through the list
items.

Any thoughts, wrong conclusions?

Cheers ... John











===== Want to unsubscribe from this list?
===== Send mail with body "unsubscribe" to macperl-request@macperl.org