tags:

views:

431

answers:

6

Is there a way to get a sub-hash? Do I need to use a hash slice?

For example:

%hash = ( a => 1, b => 2, c => 3 );

I want only

%hash = ( a => 1, b => 2 );
A: 

A hash is an unordered container, but the term slice only really makes sense in terms of an ordered container. Maybe look into using an array. Otherwise, you may just have to remove all of the elements that you don't want to produce your 'sub-hash'.

B Johnson
+6  A: 

You'd probably want to assemble a list of keys you want:

my @keys = qw(a b);

And then use a loop to make the hash:

my %hash_slice;
for(@keys) {
  $hash_slice{$_} = %hash{$_};
}

Or:

my %hash_slice = map { $_ => $hash{$_} } @keys;

(My preference is the second one, but whichever one you like is best.)

Chris Lutz
+19  A: 

Hash slices return the values associated with a list of keys. To get a hash slice you change the sigil to @ and provide a list of keys (in this case "a" and "b"):

my @items = @hash{"a", "b"};

Often you can use a quote word operator to produce the list:

my @items = @hash{qw/a b/};

You can also assign to a hash slice, so if you want a new hash that contains a subset of another hash you can say

my %new_hash;
@new_hash{qw/a b/} = @hash{qw/a b/};

Many people will use a map instead of hash slices:

my %new_hash = map { $_ => $hash{$_} } qw/a b/;
Chas. Owens
this makes why "map" works w/ hashes a little clearer to me. Thanks
Ape-inago
Larry Wall, brian d foy, and...Chas. Owens...thank you for this very comprehensive explanation!
Adam Bernier
You should probably make it more clear that a hash slice just has the values, not key/value pairs.
ysth
@ysth Are you happier now?
Chas. Owens
+1  A: 

Too much functional programming leads me to think of zip first.

With List::MoreUtils installed,

use List::MoreUtils qw(zip);

%hash = qw(a 1 b 2 c 3);
@keys = qw(a b);
@values = @hash{@keys};
%hash = zip @keys, @values;

Unfortunately, the prototype of List::MoreUtils's zip inhibits

zip @keys, @hash{@keys};

If you really want to avoid the intermediate variable, you could

zip @keys, @{[@hash{@keys}]};

Or just write your own zip without the problematic prototype. (This doesn't need List::MoreUtils at all.)

sub zip {
    my $max = -1;
    $max < $#$_and $max = $#$_ for @_;
    map { my $ix = $_; map $_->[$ix], @_; } 0..$max;
}

%hash = zip \@keys, [@hash{@keys}];


If you're going to be mutating in-place,

%hash = qw(a 1 b 2 c 3);
%keep = map +($_ => 1), qw(a b);
$keep{$a} or delete $hash{$a} while ($a, $b) = each %hash;

avoids the extra copying that the map and zip solutions incur. (Yes, mutating the hash while you're iterating over it is safe... as long as the mutation is only deleting the most recently iterated pair.)

ephemient
+2  A: 

FWIW, I use Moose::Autobox here:

my $hash = { a => 1, b => 2, c => 3, d => 4 };
$hash->hslice([qw/a b/]) # { a => 1, b => 2 };

In real life, I use this to extract "username" and "password" from a form submission, and pass that to Catalyst's $c->authenticate (which expects, in my case, a hashref containing the username and password, but nothing else).

jrockway
How is $hash->hslice(qw/a b/) better than @{$hash}{qw/a b/}? Or is this one of those cases where if I have to ask I am too far gone to understand?
Chas. Owens
You should have an answer template so that "I use Moose::" is at the start of every answer. :)
brian d foy
Chas. Owens: that's not what hslice does. @{$hash}{...} returns a list of values; hslice returns a new hash reference with both keys and values.
jrockway
+2  A: 

Yet another way:

my @keys = qw(a b);
my %hash = (a => 1, b => 2, c => 3);
my %hash_copy;
@hash_copy{@keys} = @hash{@keys};
Hynek -Pichi- Vychodil