Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://889972]
help
Chatterbox?
[atcroft]: Lady_Aleena: I found the L<name|section> syntax, which describes linking to a perl manual page, and L<text|url> to link to a web page. The reason I asked was because I was on search.cpan.org looking at a module,
[atcroft]: and it referred to several other modules, but the links went to metacpan.org. I found that a bit of a surprise, and it got me wondering if there was a "standard" way to link to other modules in the documentation on CPAN.
[Lady_Aleena]: atcroft. maybe look at raw PODs. There is also L<foo|http://foo. com> and some might be linking directly to meta::cpan.
[Lady_Aleena]: Oops, didn't read the L<text|url> right.

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2017-05-27 03:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?