tacoking92 has asked for the wisdom of the Perl Monks concerning the following question:
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!
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Matching values between two arrays of hashes.
by CountZero (Bishop) on Oct 31, 2012 at 20:48 UTC | |
Re: Matching values between two arrays of hashes.
by Kenosis (Priest) on Oct 31, 2012 at 19:11 UTC | |
by tacoking92 (Novice) on Nov 01, 2012 at 12:15 UTC | |
by Kenosis (Priest) on Nov 01, 2012 at 18:03 UTC | |
Re: Matching values between two arrays of hashes.
by space_monk (Chaplain) on Oct 31, 2012 at 18:54 UTC | |
Re: Matching values between two arrays of hashes.
by AnomalousMonk (Archbishop) on Oct 31, 2012 at 19:15 UTC | |
Re: Matching values between two arrays of hashes.
by NetWallah (Canon) on Oct 31, 2012 at 19:16 UTC |