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.
-
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.