dsb has asked for the wisdom of the Perl Monks concerning the following question:
So I was toyin' around with the range operator for a little while. The following code produced the expected.
But when I tried this...print "$_\n" for (1 .. 10);
there was no output. I don't understand why this happened since I can't see why the left operand would not be true. I figured that the loop would continue infinitely, since the right operand would never be true because of the increment. So to see if the same thing would happen with string values, I tried this...print "$_\n" for (1 .. 0);
Surprisingly, the output was a 'z'. At this point, I'm really confused. The operator is apparently not behaving the same for strings and integers. Not only that, the first time the operator is evaluated returns true, and then it becomes false after only one iteration.print "$_\n" for (z .. a);
It also says in the perlop docs that the left operand is not evaluated while the operator is in false mode, and the right operand is not evaluated while the operator is in true mode. If that is the case, how does the loop start and stop?
I know the external behavior of using something like (1 .. 10). That is, I understand what will happen. At this point, I'm really having fun looking at the internals of Perl. I'm lost on this one though.
Anyone feel like clearing this one up?
Amel - f.k.a. - kel
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Range Operators Question
by chipmunk (Parson) on Jul 19, 2001 at 00:43 UTC | |
by dsb (Chaplain) on Jul 19, 2001 at 01:26 UTC | |
by Dominus (Parson) on Jul 19, 2001 at 01:34 UTC | |
by chipmunk (Parson) on Jul 19, 2001 at 01:54 UTC | |
by dsb (Chaplain) on Jul 19, 2001 at 02:16 UTC | |
by buckaduck (Chaplain) on Jul 19, 2001 at 02:38 UTC | |
by Hofmator (Curate) on Jul 19, 2001 at 15:11 UTC | |
|
Re: Range Operators Question
by clintp (Curate) on Jul 19, 2001 at 00:47 UTC | |
|
Re: Range Operators Question
by lshatzer (Friar) on Jul 19, 2001 at 00:44 UTC | |
|
Re: Range Operators Question
by bschmer (Friar) on Jul 19, 2001 at 00:56 UTC |