Solved

Comparing two hashes

Posted on 2007-11-13
12
892 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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 video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

746 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

9 Experts available now in Live!

Get 1:1 Help Now