SPUG: Weirdness in map

Aaron West tallpeak at hotmail.com
Wed Jan 25 13:35:13 PST 2006


Nevermind.

The context within print is list context, and reverse in list context
reverses a list.

$ perl -Wle 'print reverse("asdf")'
asdf 

$ perl -Wle 'print scalar reverse("asdf")'
fdsa


-----Original Message-----
From: Aaron West [mailto:tallpeak at hotmail.com] 
Sent: Wednesday, January 25, 2006 1:29 PM
To: 'SPUG Members'
Subject: RE: SPUG: Weirdness in map

PS.

Simplified example.

$ perl -le '@a="abc";print map {reverse $_} @a'
abc

$ perl -le '@a="abc";print map {(reverse $_).""} @a'
cba


-----Original Message-----
From: spug-list-bounces+tallpeak=hotmail.com at pm.org
[mailto:spug-list-bounces+tallpeak=hotmail.com at pm.org] On Behalf Of Aaron
West
Sent: Wednesday, January 25, 2006 1:24 PM
To: 'SPUG Members'
Subject: SPUG: Weirdness in map

After the last post, I was wondering if I had any .cf files in my coldfusion
directory....and wrote this one-liner. Any idea why these have different
output?

In the first, the reverse inside the map apparently doesn't work. It's as if
sprintf is looking at the original value of $_ before it was reversed,
somehow. So I appended "" to the value returned by reverse, in the second
version, and it worked!???

========================================
Version 1:

[user at machinename] /cygdrive/c/CFusionMX
$ find . -name "*.cf*" | \
	perl -nle '($_) = split("\\.", reverse($_), 2); 
	$extr{$_}++;
	END{print map {sprintf("%s\t%d\n", reverse($_), $extr{$_}) } 
	sort keys %extr}'
cfc     19
dlo     1
dltfc   1
gfc     1
mfc     959
~1~     1

========================================
Version 2:

[user at machinename] /cygdrive/c/CFusionMX
find . -name "*.cf*" | \
	perl -nle '($_) = split("\\.", reverse($_), 2); 
	$extr{$_}++;
	END{print map {sprintf("%s\t%d\n", reverse($_) . "", $extr{$_}) } 
	sort keys %extr}'
cfc     19
old     1
cftld   1
cfg     1
cfm     959
~1~     1

========================================

$ perl -v

This is perl, v5.8.7 built for cygwin-thread-multi-64int
(with 1 registered patch, see perl -V for more detail)
..
_____________________________________________________________
Seattle Perl Users Group Mailing List  
     POST TO: spug-list at pm.org
SUBSCRIPTION: http://mail.pm.org/mailman/listinfo/spug-list
    MEETINGS: 3rd Tuesdays
    WEB PAGE: http://seattleperl.org/



More information about the spug-list mailing list