Solved

Comparing two hashes

Posted on 2007-11-13
12
904 Views
Last Modified: 2010-05-18
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
0
Comment
Question by:saibsk
  • 5
  • 4
  • 3
12 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 20275332
what are  data value and  feed_data value in your hash?
0
 

Author Comment

by:saibsk
ID: 20275405
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
0
 

Author Comment

by:saibsk
ID: 20275436

I need to find only the keys in hash1 which can be found in hash2
0
 
LVL 39

Expert Comment

by:Adam314
ID: 20275517
Is this the same as Q_22957507.html?
0
 

Author Comment

by:saibsk
ID: 20275526
yes it is the same question I thought if I need to ask a different question I need to start a new thread.
0
 
LVL 39

Expert Comment

by:Adam314
ID: 20275537
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";
    }
}
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:saibsk
ID: 20275688
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.
0
 
LVL 84

Expert Comment

by:ozo
ID: 20275718
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
0
 
LVL 39

Assisted Solution

by:Adam314
Adam314 earned 300 total points
ID: 20275725
print "$key\t$hash1{$key}\t$hash2{$key}\t" . ($hash1{$key}-$hash2{$key}) . "\n";
0
 
LVL 84

Accepted Solution

by:
ozo earned 200 total points
ID: 20275756
perl -F'\t' -lane 'BEGIN{$"="\t"}push @{$hash{"@F[0..$#F-1]"}},$F[-1];END{while(($k,$v)=each %hash){print "$k\t@$v\t",$v->[0]-$v->[1] if @$v==2}}' Hash1 Hash2
0
 

Author Comment

by:saibsk
ID: 20275800
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
0
 
LVL 39

Assisted Solution

by:Adam314
Adam314 earned 300 total points
ID: 20276134
The easiest way would be in the code that creates the hash to use a tab in between the fields instead of whatever is being used.

Without doing that:
foreach my $key (keys %hash2) {
    my ($key2=$key) =~ s/\x1c/\t/g;  #assuming all of the  are the 0x1C character
    if(!exists($hash1{$key})) {print "$key does not exist in hash1\n";}
    else {
        print "$key2\t$hash1{$key}\t$hash2{$key}\t" . ($hash1{$key}-$hash2{$key}) . "\n";
    }
}
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now