
budman
User
Feb 12, 2012, 2:14 PM
Post #6 of 6
(13285 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.
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)
|