Re: Comparing an array with hash keys
- From: japhy@xxxxxxxxxxxx (Jeff 'japhy' Pinyan)
- Date: Fri, 30 Sep 2005 10:08:51 -0400 (EDT)
On Sep 30, Mark Martin said:
I want to compare the elements of an array with the keys in a hash. If matches are found, I want to store the values associated with the matched keys in an array for future processing :
my @storage_array = ();
foreach $item(@original array) { if (exists $original_hash{$item}) {
push(@storage_array, ?????? )
}
}
Your ?????? should just be $original_hash{$item}, I expect, although this code could be written far more succinctly as a map():
my @storage_array = map {
exists $original_hash{$_} ? $original_hash{$_} : ()
} @original_array;-- Jeff "japhy" Pinyan % How can we ever be the sold short or RPI Acacia Brother #734 % the cheated, we who for every service http://www.perlmonks.org/ % have long ago been overpaid? http://princeton.pm.org/ % -- Meister Eckhart .
- References:
- Comparing an array with hash keys
- From: Mark Martin
- Comparing an array with hash keys
- Prev by Date: Comparing an array with hash keys
- Next by Date: Re: question about # of files in a directory
- Previous by thread: Comparing an array with hash keys
- Index(es):
Relevant Pages
|