Len has asked for the wisdom of the Perl Monks concerning the following question: (strings)

Can anyone explain me this:
$char = 'a'; while ($char lt 'z') { print $char; $char++; }
prints as expected the characters (a..y) but if I change lt in le in the while expression:
while ( $char le 'z' )
it prints (a..yz) instead of the expected (a..z)

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: lt versus le in string comparision
by Abigail-II (Bishop) on Jun 13, 2002 at 14:06 UTC
    Consider this:
    my $str = 'y'; $str ++; print "$str\n"; $str ++; print "$str\n";
    This will print
        z
        aa
    
    Now, both z and aa are less or equal to z. It isn't until you reach za that you have a string that isn't less or equal to z.

    Abigail