Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Comparing two hashes

Posted on 2007-11-13
12
Medium Priority
?
947 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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
 

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 900 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 600 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 900 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

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…
Six Sigma Control Plans

688 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