Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^3: Mini-Tutorial: Working with Odd/Even Elements

by duelafn (Parson)
on Jul 10, 2009 at 11:15 UTC ( [id://778888]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Mini-Tutorial: Working with Odd/Even Elements
in thread Mini-Tutorial: Working with Odd/Even Elements

Actually, I like passing the arguments so that I can do things like this: (sum doesn't know to look at $a and $b)

use List::Util qw/sum/; say for map_pairs \&sum, 1..10;

I do like the aliasing bonus, but it seems to not work on hash keys:

use YAML; my @array = ( foo_name => " Bob Smiley ", foo_age => " 32" ); map_pairs { $a =~ s/foo_//; s/^\s+//, s/\s+$// for $b; } @array; print Dump \@array; my %hash = ( foo_name => " Bob Smiley ", foo_age => " 32" ); map_pairs { $a =~ s/foo_//; s/^\s+//, s/\s+$// for $b; } %hash; print Dump \%hash;

outputs

--- - name - Bob Smiley - age - 32 --- foo_age: 32 foo_name: Bob Smiley

Good Day,
    Dean

Replies are listed 'Best First'.
Re^4: Mini-Tutorial: Working with Odd/Even Elements
by ikegami (Patriarch) on Jul 10, 2009 at 14:13 UTC

    It's not a problem with map_pairs. You'll notice the same with map and for.

    Hash keys aren't Perl variables (aren't an SV), so %hash can't possibly return an alias to them. It returns a copy.

    It could return something magical that would result in the keys being "changed", but it doesn't. One could make a tie implementation if one needed such a feature.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://778888]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (9)
As of 2024-04-24 10:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found