You have another problem... not only are you modifying copies of your data rather than the original, but you are also modifying the array you are iterating over. This often leads to unexpected results. Consider the following:

use strict; use warnings; use Data::Dumper; my @array = (1..10); for (my $i = 0; $i <= $#array; $i++) { splice(@array, $i, 1); } print Dumper(\@array);

This produces:

$VAR1 = [ 2, 4, 6, 8, 10 ];

Maybe this is what you expected, or maybe you expected the array to be empty. In either case, I don't think your program will necessarily find all the duplicates.

If I couldn't avoid creating the duplicate entries for some reason, I might merge them with something like:

use strict; use warnings; use Data::Dumper; my @players = ( { name => 'name1', deaths => 2, kills => 5, }, { name => 'name2', deaths => 2, kills => 5, }, { name => 'name3', deaths => 2, kills => 5, }, { name => 'name2', deaths => 1, kills => 4, }, ); my %merged; foreach my $player (@players) { if(exists($merged{$player->{name}})) { $merged{$player->{name}}->{deaths} += $player->{deaths}; $merged{$player->{name}}->{kills} += $player->{kills}; } else { $merged{$player->{name}}->{deaths} = $player->{deaths}; $merged{$player->{name}}->{kills} = $player->{kills}; } } print Dumper(\%merged);

which produces

$VAR1 = { 'name2' => { 'deaths' => 3, 'kills' => 9 }, 'name1' => { 'deaths' => 2, 'kills' => 5 }, 'name3' => { 'deaths' => 2, 'kills' => 5 } };

In reply to Re: Save hash value? by ig
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.