map mystery
am 23.09.2007 02:22:04 von Occidental
my @A = qw/one two three/;
print "A: ";
print join " ", @A;
print "\n";
my @A1 = map (ucfirst, @A);
print "A1: ";
print join " ", @A1;
print "\n";
my @A2 = map (reverse, @A);
print "A2: ";
print join " ", @A2;
print "\n";
my $y = reverse "one";
print "y: $y\n";
===============================================
gives:
A: one two three
A1: One Two Three
A2:
y: eno
===============================================
The first map operation works, and reverse works on a scalar, but the
array A2 is empty. Anyone know why?
Re: map mystery
am 23.09.2007 02:33:25 von xhoster
Occidental wrote:
> my @A = qw/one two three/;
>
> print "A: ";
> print join " ", @A;
> print "\n";
>
> my @A1 = map (ucfirst, @A);
> print "A1: ";
> print join " ", @A1;
> print "\n";
>
> my @A2 = map (reverse, @A);
You are reversing the empty list, which of course just gives the empty
list. The concatenation of a bunch of empty lists is still an empty list.
The reverse only operates on $_ implicitly when it is in a scalar context,
while map invokes its thingy in a list context. So put it into scalar
context explicitly:
my @A2 = map scalar reverse, @A;
--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.