Shawn H Corey wrote:
Owen wrote:
On Tue, 13 Apr 2010 05:35:51 +0200
Magne Sandøy <msan...@gmail.com> wrote:

Hi.

I'm new to perl, and I stumbled across a strange behavior in my for
loop. In the following code, the second for loop actually counts way
passed what I expected, and actually stops at "yz" and not "z" as
expected. As shown by the third for loop, incrementing the letters,
seems to give me the desired output in each loop.
What is going on here?


#!/usr/bin/perl
use warnings;
use strict;

my $letter = "u";

for ("u".."z"){
    print " $_ ";
}

print "\n\n";

for ($_="u"; $_ le "z"; $_++){
    print " $_ ";
}




In the above, when $_ gets to 'y' it is less than 'z' so it
auto increments to 'z', then when the next comparison is made, it is
equal to 'z' and so increments again.

At which point it becomes "aa", which is less than or equal to "z". It is not until it becomes "za" is it greater than "z".


Hi again.

Thanks for all the good info. I think I have a grasp on incrementing and comparison, but now, what puzzles me, is the fact that when I use "le" less than or equal, why does it actually increment the "z"? It is by then passed the less than, and already at equal to "z". It should then just exit the loop. And just to add to my confusion, "ge" and "gt" does not give any output at all. "u" is not greatier than "z", is it?



--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to