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!
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.