1 2 3 4 5 6 7 8 9 10
my %hash = ( 1=>2, 19=>20, 38=>50, 40=>10, 60=>3, 66=>80, 700=>1 ); %sums = dosomemagic(%hash); print Dumper(\%sums); #$VAR = { # '1' => 82 # '19' => 83 # '38' => 143 # '40' => 94 #}
1 2 3 4 5
my %hash = (1=>2, 19=>20, 38=>50, 40=>10, 60=>3, 66=>80, 700=>1); my @keylist = sort { $a <=> $b } keys %hash; for (my $x=0; $x<=$#keylist-3; $x++){ $hash{$keylist[$x]} += $hash{$keylist[$x+$_]} for (1..3); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
my %hash = ( 1=>2, 19=>20, 38=>50, 40=>10, 60=>3, 66=>80, 700=>1 ); my %sums = dosomemagic(\%hash); print Dumper(\%sums); #----------------- sub dosomemagic { my $hash=shift; my %return; my @keys=sort keys(%$hash); while(@keys>=4) { $return{$keys[0]}+=$hash{$keys[$_]} for(0..3); shift(@keys); } return %return; }
1
2
3
4
5
6
7
8
9
10
11
my %hash = ( 3=>12, 1=>2, 19=>20, 38=>50, 40=>10, 60=>3, 66=>80, 700=>1 );
my %sums = dosomemagic(\%hash);
print Dumper(\%sums);
#$VAR1 = {
# '38' => 143,
# '1' => 84,
# '3' => 75,
# '40' => 94,
# '19' => 92
# };