use strict; use warnings; use List::Util "reduce"; use Lingua::EN::Inflect "inflect"; use Algorithm::Loops "MapCar"; my @array = qw/A B C B D B D/; print map { map ( inflect("There PL(is,$_) $_ PL(element,$_) of "), $_->[1] ), "$_->[0]\n" } MapCar { [@_] } map { [sort keys %$_], [@$_{sort keys %$_}] } reduce { $a->{$b||$b}++; $a } {}, @array ;