I have a large hash and have a subset of keys that I want to extract the values for, without having to iterate over the hash looking for each key (as I think that will take too long).
I was wondering if I can use grep
to grab a file with a subset of keys? For example, something along the lines of:
my @value = grep { defined $hash{$_}{subsetofkeysfile} } ...;
The easiest way is to use a hash slice:
my @values_subset = @hash{@keys_subset};
If the hash may not contain some keys from the subset, use a hash slice and grep()
to filter out undef
s:
my @values_subset = grep defined, @hash{@keys_subset};