Tim Tim - 1 month ago 9
Perl Question

map for hashes in Perl

Is there a hash equivalent for

map
?

my %new_hash = hash_map { new_key($a) => new_val($b) } %hash;


I know that I could loop through the keys.

Answer

List::Pairwise claims to implement exactly that syntax -- see mapp, grepp. I haven't used it though.

Also, you can do it as

%new_hash = map { new_key($_) => new_value($hash{$_}) } keys %hash; 

which I admit looks clumsier if %hash is really a $deeply->{buried}->{hash}. I prefer using $temp = ...; map {...} keys %$temp in such cases.