in reply to Slice a hash to get a subset hash.
my %newhash; @newhash{@selection} = @oldhash{@selection};
In your example:
my %london_location; @london_location{@location_fields} = @london{ @location_fields };
Which is concise, but still repeats both the new hash and the selection.
FWIW in Perl 6 the following is supposed to work (but doesn't yet in Rakudo):
my %newhash = %oldhash{@selection}:pairs
(Update: while re-reading the specification it seem it must be :p instead of :pairs).
|
|---|
| 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 |