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:
Compare two hash of arrays in perl

 



pinky
Novice

Nov 24, 2011, 5:18 AM

Post #1 of 6 (8865 views)
Compare two hash of arrays in perl Can't Post

Hi,

How do i compare two hash of arrays in perl without using Data::Compare module?

Thanks
Pinky.

Anything wrong with the below code:


foreach my $name (keys %hash_array_cur) {
@cur_array = @{ $hash_array_cur{$name}};
foreach my $name2 (keys %hash_array_gold) {
@gold_array = @{ $hash_array_gold{$name2}};
my $temp = $name2;
$temp =~ s/^\s+//g;
$temp =~ s/\s+$//g;
next if $name !~ /$temp/;
my $temp1 = $hash_array_gold{$name2}[0];
$temp1 =~ s/^\s+//g;
$temp1 =~ s/\s+$//g;
print "golden is for $name : $temp1 \n";
print "generated is for $name : $hash_array_cur{$name}[0] \n";
if ( $hash_array_cur{$name}[0] !~ /$temp1/) {
#print "description in golden file is : $temp1 \n";
#print "description in generated file is : $hash_array_cur{$name}[0] \n";
}
}
}


(This post was edited by pinky on Nov 24, 2011, 5:19 AM)


wickedxter
User

Nov 25, 2011, 6:17 AM

Post #2 of 6 (8836 views)
Re: [pinky] Compare two hash of arrays in perl [In reply to] Can't Post

 

Code
my $hash => {Cat => [white, brown, black], 
Dog => [unknown, mut, bark],};

#loop threw hash and keys
foreach my $ref (keys %hash}){
#loop threw keys and access the array
foreach my $array (@{$hash{$ref}}){
#print array items
print $array;
}
}


this is the basic idea ....


saurabhsharma
Novice

Jan 13, 2012, 3:15 AM

Post #3 of 6 (8416 views)
Re: [pinky] Compare two hash of arrays in perl [In reply to] Can't Post

Hello Pinky,

You can try below mentioned code :

my %hash = (a=>1, b=>2, c=>3);
my %hash1 = (d=>4, e=>2, f=>5);
my %hash2 = reverse %hash1;
my %hash3 = reverse %hash;
my @present_only_in_hash1 = grep ! exists $hash2{$_}, values %hash;
my @present_only_in_hash2 = grep ! exists $hash3{$_}, values %hash1;
my @common = grep exists $hash3{$_}, values %hash1;

print "Present only in HASH 1 = @present_only_in_hash1\n";
print "Present only in HASH 2 = @present_only_in_hash2\n";
print "COMMON values = @common\n";


Cheers,

Saurabh


BillKSmith
Veteran

Jan 13, 2012, 9:10 AM

Post #4 of 6 (8394 views)
Re: [pinky] Compare two hash of arrays in perl [In reply to] Can't Post

It is hard to comment on your code when I do not know what you intend. I can describe what it does do.

It compares two hashes whose values are references to arrays. It reports a difference if both hashes have the same key, but the first element of the corresponding arrays are different. Keys are considered the same if the only difference is that the key from the 'gold' hash contains leading and/or trailing whitespace. Array elements are considered the same if the entire text of the golden one appears anywhere in the other. Keys are ignored if they appear in only one of the hashes.

Even if this is exactly what is intended, it is a poor way to do it. It uses an inner loop rather than exploiting the power of hash look-ups.
Good Luck,
Bill


rovf
Veteran

Jan 16, 2012, 5:14 AM

Post #5 of 6 (8262 views)
Re: [pinky] Compare two hash of arrays in perl [In reply to] Can't Post

Maybe Set::Scalar from CPAN?


budman
User

Feb 12, 2012, 2:14 PM

Post #6 of 6 (7627 views)
Re: [pinky] Compare two hash of arrays in perl [In reply to] Can't Post

I use something similar below (more advanced) for comparing incoming data feeds with current known column structure. vendors love to change column orders. :)

Using powers of 2 you can compare multiple arrays.


Code
my %h = ( 
tab1 => [qw(a b c d)],
tab2 => [qw(c d f g h)],
tab3 => [qw(b c d a i)],
);
my @tables = sort keys %h;

my %matches;
foreach my $t (1 .. @tables) {
$matches{$_} |= 2**$t for @{$h{ $tables[$t-1] }};
}

foreach my $m (sort keys %matches) {
my @found;
foreach my $t (1 .. @tables) {
push @found, $tables[$t-1] if $matches{$m} & 2**$t;
}
print "$m exists in ", (@found? join(",",@found) : 'None' ),"\n";
}


Output:

a exists in tab1,tab3
b exists in tab1,tab3
c exists in tab1,tab2,tab3
d exists in tab1,tab2,tab3
f exists in tab2
g exists in tab2
h exists in tab2
i exists in tab3


(This post was edited by budman on Feb 12, 2012, 2:17 PM)

 
 


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

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