CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Advanced:
Two Hash comparison

 



anbu
New User

Jul 6, 2010, 5:48 AM

Post #1 of 2 (2179 views)
Two Hash comparison Can't Post

Hi,

I have two hashes

%required = {1 => 3.11,
4 => 3.14,
3 => 3.13,
2 => 3.12};

%actual = {2 => 3.12,
1 => 3.11,
4 => 3.14};

These two hashes are recieved from a function.

Required: I need to compare the keys of these two hashes and if this is equal, I need to compare its value. For eg, If '1' in %required exists in %actual, then the value of '1' in required '3.11' should be compared with '3.11' in %actual. If, '1' doesnt exists, I need to take the next key '4' in %required and look out for '4' in %actual and proceed with its value comparison.

I also require the keys that are not present in the %actual and the values that did not match. Pls shower your thoughts.



Thanks in advance - Anbu


BillKSmith
Veteran

Jul 6, 2010, 4:00 PM

Post #2 of 2 (2161 views)
Re: [anbu] Two Hash comparison [In reply to] Can't Post

If you do not care about the order:


Code
  

use strict;
use warnings;
my %required = (
1 => 3.11,
4 => 3.14,
3 => 3.13,
2 => 3.12,
);

my %actual = (
2 => 3.12,
1 => 3.11,
4 => 3.14,
);


while (my ($key, $val) = each %required) {
print "Match key $key\n" if exists $actual{$key} and $val == $actual{$key};
}

Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives