in reply to Counting elements in array of cases
Show your code anyway?
use warnings; use strict; use Test::More; my @AoH = ( {'targetL' => 'foisonnement', 'origin' => 'AMG', 'count' => '1'}, {'targetL' => 'foisonnement', 'origin' => 'IDBR', 'count' => '1'}, {'targetL' => 'gonfler', 'origin' => 'IWWF', 'count' => '1'}, {'targetL' => 'due', 'origin' => 'IWWF', 'count' => '1' }, {'targetL' => 'due', 'origin' => 'IWWF', 'count' => '1' }, ); my @AoHfinal; my %targets; for my $h (@AoH) { push @AoHfinal, ($targets{$$h{targetL}}={targetL=>$$h{targetL}}) unless $targets{$$h{targetL}}; $targets{$$h{targetL}}{origin}{$$h{origin}}++; $targets{$$h{targetL}}{count}++; } $$_{origin} = join ' ', sort keys %{$$_{origin}} for values %targets; is_deeply \@AoHfinal, [ {'targetL' => 'foisonnement','origin' => 'AMG IDBR','count'=>'2'}, {'targetL' => 'gonfler','origin' => 'IWWF','count' => '1'}, {'targetL' => 'due','origin' => 'IWWF','count' => '2'}, ] or diag explain \@AoHfinal; done_testing;
Note you didn't specify the order of the resulting values, both @AoHfinal (so I kept the original order from @AoH), or of the origin values (so I sorted them because it was easier).
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Counting elements in array of cases
by Anonymous Monk on Sep 27, 2019 at 08:37 UTC |