Apologies if either of these have been answered elsewhere and I just hadn't found them ...
First, according to the documentation for the range operator "..":
In list context, it returns a list of values counting (up by ones) from the left value to the right value. If the left value is greater than the right value then it returns the empty list.
So how in the world is the code:
#!/usr/bin/perl -w use strict; use warnings; my @range = ('e' .. 'd'); printf "%s\n", join(" ", @range);
Resulting in this:
e f g h i j k l m n o p q r s t u v w x y z
?
Second, with this code:
#!/usr/bin/perl -w use strict; use warnings; sub letters { foreach my $letter ('a' .. 'c', 'd' .. 'f') { print " $letter"; $letter = uc $letter; } print "\n"; } letters(); letters(); letters();
the results are truly not what I expect(ed):
a b c d e f A B C D E F A B C D E F
The first time through, the letters are printed in lower-case, but subsequent passes indicate the line:
has somehow modified the list. Can that really be what has happened?$letter = uc $letter;
It doesn't seem to have the same effect as:
sub letters { foreach my $letter ('a' .. 'f') { print " $letter"; $letter = uc $letter; } print "\n"; } # Prints... a b c d e f a b c d e f a b c d e f
which behaves as I would have thought.
Nor does constructing the array first have the apparent aberrant outcome:
sub letters { my @letters = ('a' .. 'c', 'd' .. 'f'); foreach my $letter (@letters) { print " $letter"; $letter = uc $letter; } print "\n"; } # Prints... a b c d e f a b c d e f a b c d e f
The results were the same both with Perl 5.8.5 on RedHat EL4.3, and Perl 5.10.0 on Windows XP.
Can anyone enlighten me?
In reply to Two Range Operator Anomalies by liverpole
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |