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.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
Outside of code tags, you may need to use entities for some characters:
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.