Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^3: multiple hash compare, find, create

by Eily (Monsignor)
on Dec 10, 2018 at 23:57 UTC ( [id://1227072]=note: print w/replies, xml ) Need Help??


in reply to Re^2: multiple hash compare, find, create
in thread multiple hash compare, find, create

I don't know how time sensitive your project is, but even if you can afford to see your program run for 10 minutes, it's always nice to have something faster than that :D. You can actually speed things up a lot by *not* building the three hashes. johngg had a good point when he said that you should base your search on the smallest hash. Going even further you only need to build that one. This would give something like (that's pseudo-perl, but you should get the idea):

my %h1 = $csv1->csv_to_hash; # with $csv1 the smallest file my %temp; while (my $line = $csv2->next) { my $key = $line->key; # Ignore lines that don't also exist in h1 # That's way less data to build into the second hash next unless exists $h1{$key}; my $value = $line->value; $temp{ $key } = [ $h1{$key}, $value ]; } %h1 = (); # Free the data from %h1, the matching pairs are all in %tem +p anyway my %out; while (my $line = $csv3->next) { my $key = $line->key; # As before, ignore non relevant keys next unless exists $temp{$key}; # We make a copy of the array ref from %temp # This means that technically modifying it also changes the content +of %temp # But it will be deleted anyway my $values_array = $temp{$key}; my $value = $line->value; push @$values_array, $value; # Add the values into a brand new hash # So that it contains only the needed keys $out{$key} = $values_array; } %temp = (); # The important keys have been copied to %out;

You should also consider using Text::CSV if you're not already doing so :)

Edit: oh, and I showed the wrong example, but you should avoid variable names that are identical except for the number as much as possible. So maybe rename %h1 to %ref, if you don't already have better (distinct) names available for your hashes.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1227072]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2024-04-23 07:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found