Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Counting elements in array of cases

by haukex (Archbishop)
on Sep 27, 2019 at 08:32 UTC ( [id://11106780]=note: print w/replies, xml ) Need Help??


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
    Aww you fell for it and did OPs homework

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11106780]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2024-04-25 07:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found