The simplest approach is to use a temporary array but you could splice if memory is an issue.
$var = [ { 'id_a' => '1', 'id_b' => '5', 'value_x' => '10', 'value_y' => '5', }, { 'id_a' => '2', 'id_b' => '3', 'value_x' => '20', 'value_y' => '10', }, { 'id_a' => '2', 'id_b' => '3', 'value_x' => '30', 'value_y' => '20', }, { 'id_a' => '3', 'id_b' => '7', 'value_x' => '15', 'value_y' => '15', }, ]; my $tmp; my $last_id = ''; for my $hash( @$var ) { if ( $hash->{id_a} eq $last_id ) { $tmp->[-1]->{value_x} += $hash->{value_x}; $tmp->[-1]->{value_y} += $hash->{value_y}; } else { $last_id = $hash->{id_a}; push @$tmp, $hash; } } use Data::Dumper; print Dumper $tmp;
cheers
tachyon
In reply to Re: Messing about in Arrays of Hashes
by tachyon
in thread Messing about in Arrays of Hashes
by nzgrover
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |