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

    Thank you.

    I asked the question because It just felt as if there ought to be something in the language to slice hashes. It is interesting that it is a planned feature for Perl 6. I guess that someone agrees that the feature should be there.