Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Re: Slice a hash to get a subset hash.

by ELISHEVA (Prior)
on Feb 24, 2011 at 12:03 UTC ( #889972=note: print w/replies, xml ) Need Help??

in reply to Slice a hash to get a subset hash.

What I usually do is something like this (your final solution in the OP):

my %location = map { $_ => $london{$_} } qw(Long Lat Elevation);

However, if that isn't clean enough for you, you could use prototypes to create a bit of syntactic sugar:

sub extract(\%@) { my $h = shift; map { $_ => $h->{$_} } @_; } # usage my %location = extract %london, qw(Long Lat Elevation); my %stats = extract %london, 'Population', 'Area'

Replies are listed 'Best First'.
Re^2: Slice a hash to get a subset hash.
by chrestomanci (Priest) on Feb 24, 2011 at 14:41 UTC

    Thank you, that is a nice bit of syntactic sugar. I would add it to my toolbox, but I suspect that if I did I would be for ever explaining it to the other perl programers at $work.

    I think I will stick with the map method that I put in the original post. It is good enough.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://889972]
[Corion]: Actually bsd_glob '{www.,}perlmonks .{com,net,org}', plus I think
[Corion]: Sad that Let's Encrypt does not allow wildcard certificates, but they could be somewhat of a hassle to verify
[Discipulus]: since 2002 I just usewd and good morning
Discipulus .oO(maybe a PM discussion?)

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (8)
As of 2017-09-26 07:52 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (292 votes). Check out past polls.