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. I suspect auto increment is different for letters and numbers > print "\n\n"; > > for(1..7){ > print " $letter "; > $letter++; > } Which goes to show that the thing after 'z' is 'aa' using the auto increment Owen -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/