Perl, how to write all hash values?

I need to copy the (single-level) hash to a new one, with all the values ​​below.

Do you know a smart method (just to avoid the ugly foreach ... ;-)

+5
source share
5 answers

In a fun spirit, here's a solution using a somewhat obscure feature each. (Do not believe that I have ever used it before.)

$new{$key} = lc $val while ($key,$val) = each %old;
+2
source
my %new = map { $_ => lc $old{$_} } keys %old;
+13
source

map:

my %newHash = map { $_ => lc $existingHash{$_} } keys %existingHash;
+6

, :

my %new_hash;
@new_hash{keys %old_hash} = map lc, values %old_hash;

, keys values , , , .

+6

( , , ).

my %new = %old;
$_ = lc for values %new;
+3
source

All Articles