http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> I have got multiple hashes in an array with same keys. And I want to

> count the total of values with same keys.

>

> hash1

>

> score1 20

> score2 10

> score3 20

>

>

> hash2

>

> score1 30

> score2 20

> score3 30
What does that mean? Does it mean this structure?

my @notesw = (

{

'score1' => 20,

'score2' => 10,

'score3' => 20,

},

{

'score1' => 30,

'score2' => 20,

'score3' => 30,

},

);

> I am looking for out put 'Score1 Total = 50; Score2 Total = 30;

> Score1 Total = 50; '

>

> I have tried the following loop, ended up with some strange

> numbers..........
"Some strange numbers" is a terribly bad description of the actual output.

> advice please

>

> my $sum = 0;

> for my $m (0 .. $#notesw){

> for $variable (keys %{$notesw[$m]}) {

> print "$variable = ${notesw[$m]{$variable}}\n";

> $sum += ${notesw[$m]{$variable}};

> print "$sum \n";

> }

> }
Well, you need 3 sums, not just 1, right?

Maybe something like this is what you want:

my ( $sum1, $sum2, $sum3 );

for my $hashref ( @notesw ) {

$sum1 += $hashref->{'score1'};

$sum2 += $hashref->{'score2'};

$sum3 += $hashref->{'score3'};

}

print "Score1 Total = $sum1\n",

"Score2 Total = $sum2\n",

"Score3 Total = $sum3\n";

--

Gunnar Hjalmarsson

Email:

http://www.gunnar.cc/cgi-bin/contact.pl