saibsk
asked on
Comparing two hashes
I have two hashes data and feed_data with the same type of data: date, account number, currency forms the key and balance forms the values. I need to compare the keys and when they match I need to subtract the "data" value from the "feed_data" value. And display the key with the data value, feed_data value and difference between them
what are data value and feed_data value in your hash?
ASKER
Hash 1:
20071102 00238923 Joh Smith 599596.75
20071102 00238791 Davis 1.23
20071102 00238790 Harry 400
Hash 2:
20071102 00238790 Joh Smith 599587.6
20071102 00238790 Harry 305
The first 3 columsn form the key while the last column is the value for the hashes which is a decimal number. I need to find all the hash keys in hash 1 found in hash2. Then find they value difference.
It should print to a file something like
20071102 00238923 Joh Smith 599596.75 599587.6 9.15
20071102 00238790 Harry 400 300 100
20071102 00238923 Joh Smith 599596.75
20071102 00238791 Davis 1.23
20071102 00238790 Harry 400
Hash 2:
20071102 00238790 Joh Smith 599587.6
20071102 00238790 Harry 305
The first 3 columsn form the key while the last column is the value for the hashes which is a decimal number. I need to find all the hash keys in hash 1 found in hash2. Then find they value difference.
It should print to a file something like
20071102 00238923 Joh Smith 599596.75 599587.6 9.15
20071102 00238790 Harry 400 300 100
ASKER
I need to find only the keys in hash1 which can be found in hash2
Is this the same as Q_22957507.html?
ASKER
yes it is the same question I thought if I need to ask a different question I need to start a new thread.
foreach my $key (keys %hash2) {
if(!exists($hash1{$key})) {print "$key does not exist in hash1\n";}
else {
print "$key $hash1{$key} $hash2{$key} " . ($hash1{$key}-$hash2{$key} ) . "\n";
}
}
if(!exists($hash1{$key})) {print "$key does not exist in hash1\n";}
else {
print "$key $hash1{$key} $hash2{$key} " . ($hash1{$key}-$hash2{$key}
}
}
ASKER
2007110200205321Jon Smith 0
2007110200205321Harry -2506535.69
the data is printed something like this. How do I add tab delimiters between the keys. as I want the data printed to tab delimited.
2007110200205321Harry -2506535.69
the data is printed something like this. How do I add tab delimiters between the keys. as I want the data printed to tab delimited.
perl -lane 'push @{$hash{"@F[0..$#F-1]"}},$ F[-1];END{ while(($k, $v)=each %hash){print "$k @$v ",$v->[0]-$v->[1] if @$v==2}}' Hash1 Hash2
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I want tabs within the keys ie the key is now:
2007110200205321Jon Smith i want it to be to be tab delimited to
20071102 00205321 Jon Smith
2007110200205321Jon Smith i want it to be to be tab delimited to
20071102 00205321 Jon Smith
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.