Search your unanswered perl queries
Google

Thursday, October 30, 2008

How to take a hash slice.

If you want to assign some variables of a hash to an array. i.e you want to take some of the elements from hash based on there keys into a array here is a trick\

my %hash = (one => 'one', two => 'two', three => 'three');
my @array ;
@array = @{$hash}{'one','two'};

If you have a hashref and want to take the values of some keys into an array you can do the following.

my $hashref = {one => 'one', two => 'two', three => 'three'};
my @array ;
@array = @{%$hash}{'one','two'};

No comments: