agoth has asked for the wisdom of the Perl Monks concerning the following question:
my %hash1 = ('one' => 'un', 'two' => 'deux', 'three' => 'trois'); # f +ixed my @ary = ('one', 'two', 'four'); # v +ariable #----------------------------------------- for (keys %hash1) { my $loc = $_; my $mat = 0; for (@ary) { $mat = 1 if ($_ eq $loc); } delete $hash1{$loc} unless $mat; } for (keys %hash1) { print "**$_** => **$hash1{$_}**\n"; } #----------------------------------------- my %hash2 = map { $_ => $hash1{$_} if (defined $hash1{$_}) } @ary; for (keys %hash2) { print "**$_** => **$hash2{$_}**\n"; } #-----------------------------------------
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Mapping array over a hash
by Masem (Monsignor) on Jul 20, 2001 at 17:56 UTC | |
by agoth (Chaplain) on Jul 20, 2001 at 18:02 UTC | |
|
Re (tilly) 1: Mapping array over a hash
by tilly (Archbishop) on Jul 20, 2001 at 18:22 UTC | |
|
Re: Mapping array over a hash
by arturo (Vicar) on Jul 20, 2001 at 18:03 UTC | |
by japhy (Canon) on Jul 20, 2001 at 18:38 UTC | |
by larryk (Friar) on Jul 20, 2001 at 19:29 UTC | |
by japhy (Canon) on Jul 20, 2001 at 19:46 UTC | |
|
Re: Mapping array over a hash
by dragonchild (Archbishop) on Jul 20, 2001 at 17:56 UTC | |
by agoth (Chaplain) on Jul 20, 2001 at 18:02 UTC | |
by dragonchild (Archbishop) on Jul 20, 2001 at 18:09 UTC |