in reply to Comparing values in same hash?
#!/usr/bin/perl -w use strict; my %hash= ( 'ID1' => { 'myStr' => 'hello', 'description' => 'Description 1', 'yourStr' => [ 'goodbye', 'where' ] }, 'ID2' => { 'myStr' => 'good', 'description' => 'Description 2', 'yourStr' => [ 'hello', ] }, 'ID3' => { 'myStr' => 'testvar', 'description' => 'Description 2', 'yourStr' => [ 'hello', 'good' ] }, ); foreach my $id (keys %hash) { my $mystr = $hash{$id}{myStr}; foreach my $compareid (grep{$_ ne $id}keys %hash) { if (grep{ $_ eq $mystr} @{$hash{$compareid}{yourStr}}) { print "$id myStr=$mystr found in $compareid yourStr=@{$hash +{$compareid}{yourStr}}\n"; } } } __END__ ID1 myStr=hello found in ID3 yourStr=hello good ID1 myStr=hello found in ID2 yourStr=hello ID2 myStr=good found in ID3 yourStr=hello good
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Comparing values in same hash?
by legendx (Acolyte) on Jul 24, 2011 at 17:20 UTC |