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
Solved

Comparing two hashes

Posted on 2007-11-13
12
918 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…

856 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