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: Re: [pinky] Compare two hash of arrays in perl: Edit Log



budman
User

Feb 12, 2012, 2:14 PM


Views: 8216
Re: [pinky] Compare two hash of arrays in perl

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)


Edit Log:
Post edited by budman (User) 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