in reply to Self-Looping over hash - how to remove duplicate
Just check if the A-B exists before adding B-A.
my %hash2 =( '1' => 2, '3' => 5, '4' => 2, ); my %spair = (); for my $si (keys %hash2) { for my $sn (keys %hash2) { next if exists $spair{$sn."-".$si}; $spair{$si."-".$sn} = ($si == $sn)? $hash2{$si} : $hash2{$si}* +$hash2{$sn}; } } use Data::Dumper; print Dumper \%spair; __DATA__ $VAR1 = { '3-3' => 5, '1-1' => 2, '3-4' => '10', '1-3' => '10', '1-4' => '4', '4-4' => 2 };
cheers
tachyon
|
|---|