http://qs321.pair.com?node_id=541598

AcidHawk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have 2 csv files, File1 look as follows

1141286452,ServerA,Disk Full,Arb data,other,stuff
1141286737,ServerB,Net Down,Arb data,other,stuff
1141286737,ServerC,Disk Full,Arb data,other,stuff

and File2 like so

1141286737,ServerB,Net Down
1141286780,ServerD,Bit Bucket Missing

I read them into 2 seperate hashes. (In the example code I have just hardcoded the hashes.) Firstly, I need each line of the file to appear in the hash, and none of the fields contain UNIQUE data, hence the index (or KEY of Ahash and Bhash) I create ie (1,2,3 etc which relates to line numbers). The real data is held in the value of Ahash and Bhash as another hash. Looking at the example code may help with the explanation.

What I want to do is check if a VALUE in Ahash is also in Bhash (or a line in File1 is also in File2). So here is what I have that works on small hashes...

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %Ahash = ( 1 => { 'Epoch' => 1234567, 'Node' => "ServerA", 'Event' => "Disk Full", 'other' => "Arb data", }, 2 => { 'Epoch' => 1234570, 'Node' => "ServerB", 'Event' => "Net Down", 'other' => "Arb data", }, 3 => { 'Epoch' => 1234580, 'Node' => "ServerC", 'Event' => "Screen Stolen", 'other' => "Arb data", }, ); my %Bhash = ( 1 => { 'Epoch' => 1234530, 'Node' => "ServerD", 'Event' => "Bit Bucket Missing",}, 2 => { 'Epoch' => 1234567, 'Node' => "ServerA", 'Event' => "Disk Full",}, ); #print Dumper (%Ahash); #print "===================================\n"; #print Dumper (%Bhash); while ( (my $Akey, my $Avalue) = each %Ahash) { while ( (my $Bkey, my $Bvalue) = each %Bhash) { if ( $$Bvalue{'Epoch'} eq $$Avalue{'Epoch'} && $$Bvalue{'Node'} eq $$Avalue{'Node'} && $$Bvalue{'Event'} eq $$Avalue{'Event'} ) { print ">>>>>>>MATCHED\n"; print "Ahash => " . Dumper($Avalue); print "Bhash => " . Dumper($Bvalue); print "MATCHED<<<<<<<<<<<<<<\n"; } else { print "NOT MATCHED\n"; } } }

This does not scale well as you can see that each value in Ahash is checked agains each value in Bhash. Is there a better way to compare values in hashes that are hashes?

-----
Of all the things I've lost in my life, its my mind I miss the most.