array of hashes

  • Thread starter SrikanthMandava2004
  • Start date
S

SrikanthMandava2004

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

Please note that i have multiple hashes not two as above shown. I am
looking for out put 'Score1 Total = 50; Score2 Total = 30; Score1
Total = 50; ' Looking for the output on the basis of having same key.

I am a perl newbie, have tried the following loop, ended up with some
strange numbers..........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";
}
}
 
X

xhoster

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

Please note that i have multiple hashes not two as above shown. I am
looking for out put 'Score1 Total = 50; Score2 Total = 30; Score1
Total = 50; ' Looking for the output on the basis of having same key.

I am a perl newbie, have tried the following loop, ended up with some
strange numbers..........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";
}
}

my %uberhash;
foreach (@notesw) {
while (my ($k,$v)=each %$_) {
$uberhash{$k}+=$v
};
};
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top