in reply to iterating 2 arrays
G'day sunil9009,
Another issue you have here is that you're checking all the keys for a match with the first element of @ValidLocations, then checking all the keys again for a match with the next element of @ValidLocations. This means you're performing a lot of unnecessary processing: assuming the data you've posted is just a sample of the real dataset, this could be a substantial amount of unnecessary processing.
Consider coding your solution more like this:
#!/usr/bin/env perl -l use strict; use warnings; my @ValidLocations = ('Lynnwood','Edmonds'); my $ref = { 'US/Seattle/WA' => ['Smith','Eric','Sam'], 'US/WA/Lynnwood' => ['John','Chuck','Ram','Lynda'], 'US/WA/Everette' => ['Sun','Rick','Raj'], 'US/Edmonds/WA' => ['Ken','Josh','Matt'], 'IN/Banglore/KA' => ['Anil','Dada','Kaka'] }; my $re = '^US.+?(?:' . join('|' => @ValidLocations) . ')'; my @valid_us_people = map { @{$ref->{$_}} } grep { /$re/ } keys %$ref; print "@valid_us_people";
Output:
John Chuck Ram Lynda Ken Josh Matt
You may already have your data as a hashref. If not, and you choose to change $ref to %hash (as suggested by GrandFather in the first reply), just change $ref->{$_} to $hash{$_} and %$ref to %hash.
-- Ken
|
|---|