I have two array of hashes that have a key value that is the same between them. They key value is 'location_id'. I want to match these arrays of hashes against the key value and return the 'id' value of one of the hashes if it matches. I currently have working code, but I do not think it is correct or the fastest way to accomplish this. Example:
@subnets = { { 'id' => '1', 'location_id' => '30', 'subnet' => '255.255.255.0' }, { 'id' => '2', 'location_id' => '13', 'subnet' => '255.255.254.0' }, { 'id' => '3', 'location_id' => '19', 'subnet' => '255.255.0.0' }, }; @filers_info = { { 'id' => '1', 'location_id => '19', 'info' => 'blah1', }, { 'id' => '2', 'location_id => '30', 'info' => 'blah1', }, }; foreach my $subnet (@subnets) { my @found_filers = grep { $_->{'location_id'} == $subnet->{'locatio +n_id'} } @filers_info; push @total_filers, @found_filers; } @filer_ids = map{$_->{'id'} } @total_filers;
This does work as expected, but I don't think it's fast enough. In the real world @subnets contains over 100,000 records and @filers_info has around 2000. This looping through the @subnets array takes quite a while. I'm looking for a method to possibly speed this code up without using any external modules. Thanks!
In reply to Matching values between two arrays of hashes. by tacoking92
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |