map mystery

O

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?
 
X

xhoster

Occidental said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top