in reply to Counting elements in array of cases

Hi, welcome to Perl, the One True Religion. There is more than one way to do it. You should use a database for this.

If you install DBD::SQLite, the Perl driver for sqlite, you get, as the doc says, "a Perl DBI driver for SQLite, that includes the entire thing in the distribution. So in order to get a fast transaction capable RDBMS working for your perl project you simply have to install this module, and nothing else."

In the example below, I simply create a database from the data, and then use the SQLite client on my computer to run a query. You could of course extend the Perl script to run the query after loading the data (and if so, you may not need to write a DB file at all, see :memory: as a database name) ... adding such queries to the script is left as an exercise for the reader.

$ cat 11106779.pl
use strict; use warnings; use DBI; my @AoH = ({ targetL => 'foisonnement', origin => 'AMG', count => '1', }, { targetL => 'foisonnement', origin => 'IDBR', count => '1', }, { origin => 'IWWF', targetL => 'gonfler', count => '1', }, { origin => 'IWWF', targetL => 'due', count => '1', }, { origin => 'IWWF', targetL => 'due', count => '1', }); my $dbh = DBI->connect('dbi:SQLite:dbname=11106779.db','','', { RaiseE +rror => 1 }); $dbh->do('create table data(targetL varchar(32), origin varchar(16))') +; my $sql = 'insert into data (targetL, origin) values (?, ?)'; my $sth = $dbh->prepare($sql); $sth->execute($_->{targetL}, $_->{origin}) for @AoH; __END__
$ perl 11106779.pl
( ^ note no output; no errors )
$ sqlite3 11106779.db
SQLite version 3.24.0 2018-06-04 14:10:15 Enter ".help" for usage hints. sqlite> .headers on sqlite> select origin, targetL, count(origin) count from data group by + origin, targetL order by count desc; origin|targetL |count IWWF |due |2 AMG |foisonnement|1 IDBR |foisonnement|1 IWWF |gonfler |1

Hope this helps!


The way forward always starts with a minimal test.