Since you don't want to have names repeated. The easiest thing is to convert your array of hashes (AoH) into a hash of hash (HoH). The first part of the code translates your AoH into a HoH. Then I show how to add somebody new (mary) to the structure. You can use this way to create the structure from scratch. (e.g. no real need for the AoH structure unless you need it for some other reason).
#!/usr/bin/perl -w use strict; use Data::Dump qw(pp); my @players = ( {name => 'Bob', deaths => 2, kills => 5}, {name => 'Bennie', deaths => 1, kills =>13}, {name => 'Bob', deaths => 10, kills => 10}, {name => 'Jane', deaths => 0, kills =>30} ); my %playerHoH; foreach my $href (@players) { my $name = $href->{name}; $playerHoH{$name}{deaths} += $href->{deaths}; $playerHoH{$name}{kills} += $href->{kills}; } # adding new player is easy... same case as adding deaths and # kills to an existing player. # $palyerHoH{name} will be created if it doesn't already exist. $playerHoH{mary}{deaths} += 4; $playerHoH{mary}{kills} += 20; print '%playerHOH = ',pp(\%playerHoH),"\n"; __END__ Prints: \%playerHOH = { Bennie => { deaths => 1, kills => 13 }, Bob => { deaths => 12, kills => 15 }, Jane => { deaths => 0, kills => 30 }, mary => { deaths => 4, kills => 20 }, }
Update: for completeness, I added how to convert the HoH back to AoH. See below..

#!/usr/bin/perl -w use strict; use Data::Dump qw(pp); my %playerHoH = ( Bennie => { deaths => 1, kills => 13 }, Bob => { deaths => 12, kills => 15 }, Jane => { deaths => 0, kills => 30 }, mary => { deaths => 4, kills => 20 }, ); my @AoH; foreach my $name (sort keys %playerHoH) { # the {} means allocate memory for a new anon hash # it is populated and a reference to that anon hash # is pushed on to the @AoH. push @AoH, { name => $name, deaths=> $playerHoH{$name}{deaths}, kills => $playerHoH{$name}{kills} }; } print '@AoH=' ,pp(\@AoH), "\n"; __END__ prints: @AoH=[ { deaths => 1, kills => 13, name => "Bennie" }, { deaths => 12, kills => 15, name => "Bob" }, { deaths => 0, kills => 30, name => "Jane" }, { deaths => 4, kills => 20, name => "mary" }, ]

In reply to Re: Save hash value? by Marshall
in thread Save hash value? by toxicious

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.