SPUG: Weirdness in map

Darren Stalder torin.darren at gmail.com
Wed Jan 25 13:52:46 PST 2006


In the first case, the reverse is in list context and reverses the
list of ($_).  The reverse of a list with one element is that same
list.  Once you append "" to reverse($_), you're putting it into
scalar context where reverse does what you're expecting.

Darren

On 1/25/06, Aaron West <tallpeak at hotmail.com> wrote:
> 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