monkfan has asked for the wisdom of the Perl Monks concerning the following question:
Where the key is index, and the values is the count of that index.my %hash2 =( '1' => 2, '3' => 5, '4' => 2,);
My current code:$VAR1 = { '3-3' => 5, '3-4' => 10, '1-1' => 2, '3-1' => 10, '1-4' => 4, '4-4' => 2,#ends here };
give this which is not as desired:use strict; use warnings; use Data::Dumper; #print Dumper \%hash2; my %spair = (); my $prod; foreach my $si (keys %hash2) { foreach my $sn (keys %hash2) { if ($si == $sn) { $prod = $hash2{$si}; } else { $prod = $hash2{$si}*$hash2{$sn}; } $spair{$si."-".$sn} = $prod; } } print Dumper \%spair;
As you can see it repeats 1-4 and 4-1, etc. which actually should only occur once. How can I modify my code so that we can get the expected result?$VAR1 = { '3-3' => 5, '3-4' => 10, '1-1' => 2, '3-1' => 10, '1-4' => 4, '4-4' => 2, '4-1' => 4, #repeat '1-3' => 10,#repeat '4-3' => 10 #repeat };
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Self-Looping over hash - how to remove duplicate
by tachyon (Chancellor) on Oct 05, 2004 at 08:07 UTC | |
|
Re: Self-Looping over hash - how to remove duplicate
by gothic_mallard (Pilgrim) on Oct 05, 2004 at 08:16 UTC | |
|
Re: Self-Looping over hash - how to remove duplicate
by gaal (Parson) on Oct 05, 2004 at 08:07 UTC | |
by monkfan (Curate) on Oct 05, 2004 at 08:20 UTC | |
|
Re: Self-Looping over hash - how to remove duplicate
by atcroft (Abbot) on Oct 05, 2004 at 08:08 UTC | |
|
Re: Self-Looping over hash - how to remove duplicate
by Hena (Friar) on Oct 05, 2004 at 08:14 UTC | |
by Jasper (Chaplain) on Oct 05, 2004 at 09:20 UTC | |
|
Re: Self-Looping over hash - how to remove duplicate
by Anonymous Monk on Oct 05, 2004 at 12:25 UTC |