in reply to Self-Looping over hash - how to remove duplicate

You could have both iterations go over the hash in an ordered manner, and then quit the inner loop after processing the key against itself. This is not tested:

use strict; use warnings; #print Dumper \%hash2; my %spair = (); my $prod; foreach my $si (sort { $a <=> $b } keys %hash2) { INNER: foreach my $sn (sort { $a <=> $b } keys %hash2) { if ($si == $sn) { $prod = $hash2{$si}; $spair{$si."-".$sn} = $prod; #: $hash2{$si}\n"; } else { $prod = $hash2{$si}*$hash2{$sn}; $spair{$si."-".$sn} = $prod; #: $hash2{$si}\n"; last INNER; } } } print Dumper \%spair;

Replies are listed 'Best First'.
Re^2: Self-Looping over hash - how to remove duplicate
by monkfan (Curate) on Oct 05, 2004 at 08:20 UTC
    Hi, thanks for the reply,
    but your code gives:
    $VAR1 = { '1-1' => 2, '3-1' => 10, '4-1' => 4, '1-3' => 10 };

    Notice that 2-2, 3-3, and 4-4 is not included