Solved

Comparing two hashes

Posted on 2007-11-13
12
911 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
perl to display a matrix report for 3-dimenional array 3 67
transpose into pipe delemited 8 68
combine multiple lines 2 59
parse a file and get data out 11 44
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 …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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 hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

776 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