array of hashes

Discussion in 'Perl Misc' started by SrikanthMandava2004@gmail.com, Feb 19, 2007.

  1. Guest

    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";
    }
    }
     
    , Feb 19, 2007
    #1
    1. Advertising

  2. Guest

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

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Feb 19, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Holness

    Hashes of Hashes via subs

    Ben Holness, Oct 5, 2003, in forum: Perl
    Replies:
    8
    Views:
    590
    Ben Holness
    Oct 8, 2003
  2. Steven Arnold

    using hashes as keys in hashes

    Steven Arnold, Nov 23, 2005, in forum: Ruby
    Replies:
    3
    Views:
    183
    Mauricio Fernández
    Nov 23, 2005
  3. kazaam
    Replies:
    12
    Views:
    294
    Matthias Wächter
    Sep 13, 2007
  4. Matt Brooks
    Replies:
    16
    Views:
    251
    Matt Brooks
    Sep 16, 2009
  5. Tim O'Donovan

    Hash of hashes, of hashes, of arrays of hashes

    Tim O'Donovan, Oct 27, 2005, in forum: Perl Misc
    Replies:
    5
    Views:
    235
Loading...

Share This Page