Preserves order of both targetL and origin.
#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11106779 use warnings; use List::Util qw( uniq ); my @AoH = ( { count => 1, origin => "AMG", targetL => "foisonnement" }, { count => 1, origin => "IDBR", targetL => "foisonnement" }, { count => 1, origin => "IWWF", targetL => "gonfler" }, { count => 1, origin => "IWWF", targetL => "due" }, { count => 1, origin => "IWWF", targetL => "due" }, ); my %seen; my @AoHfinal = grep { if( my $prev = $seen{$_->{targetL}} ) { $prev->{count} += $_->{count}; $prev->{origin} = join ' ', uniq split(' ', $prev->{origin}), $_-> +{origin}; 0 # skip duplicate } else { $seen{$_->{targetL}} = $_; } } @AoH; use Data::Dump 'dd'; dd @AoHfinal;
Outputs:
( { count => 2, origin => "AMG IDBR", targetL => "foisonnement" }, { count => 1, origin => "IWWF", targetL => "gonfler" }, { count => 2, origin => "IWWF", targetL => "due" }, )
In reply to Re: Counting elements in array of cases
by tybalt89
in thread Counting elements in array of cases
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |