Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > array of hashes

Reply
Thread Tools

array of hashes

 
 
SrikanthMandava2004@gmail.com
Guest
Posts: n/a
 
      02-19-2007
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; '

I 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";
}
}

 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      02-19-2007
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
 
Reply With Quote
 
 
 
 
SrikanthMandava2004@gmail.com
Guest
Posts: n/a
 
      02-19-2007
Thanks for the advice.

On Feb 19, 3:07 pm, Gunnar Hjalmarsson <(E-Mail Removed)> wrote:
> (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,
> },
> );


Yes, I will follow that for my future queries.

> > 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


I had out put in 5 digits like 25323 or something ....

> > 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";



The above hashes are just examples, however files I am working on have
hash value with spaces eg: 'Total data count' which is creating
'Use of uninitialized value in addition (+)' error. Not sure whether I
still need to use the for loop of 'for my $m (0 .. $#notesw)' with the
logic you provided.

I appreciate the help, thanks.
> --
> Gunnar Hjalmarsson
> Email:http://www.gunnar.cc/cgi-bin/contact.pl



 
Reply With Quote
 
SrikanthMandava2004@gmail.com
Guest
Posts: n/a
 
      02-19-2007
Gunnar Hjalmarsson

Ignore my previous response as newbie's panic reply.

Thanks for your help, managed to solve that. And I need better
understanding of references, and thats my next task.

thanks,

Sri

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Array of Hashes in an array of hashes - Complicated! Matt Brooks Ruby 16 09-16-2009 05:53 PM
How to make an array of hashes to a single array with all thevalues of these hashes ? kazaam Ruby 12 09-13-2007 01:30 PM
using hashes as keys in hashes Steven Arnold Ruby 3 11-23-2005 03:25 PM
Hash of hashes, of hashes, of arrays of hashes Tim O'Donovan Perl Misc 5 10-28-2005 05:59 AM
Hashes of Hashes via subs Ben Holness Perl 8 10-08-2003 06:57 AM



Advertisments