Hi I have a hash where the key is a 3digit code and value is number of elements under that code. I want to sum the 3digit code and multiply with the number of elements, then finally add them up. Example:
000 23
012 42
222 34
[(0+0+0)*23]+[(0+1+2)*42]+[(2+2+2)*34]=0+126+204=330
so i tried
foreach my $key (sort keys %hash ){
@arrSum=split(//, $key);
foreach $i (@arrSum){
$sum+=$i;
$value=$sum*$hash{$key};
}
}
It does not work. It actually has to consider each line, instead it sums up all the 3digit code at once. Need some help.
Here is more functional approach to the problem, it also uses
sumfrom List::Util: