my %h; @h{@a} = (); # make a set of the unique strings for my $k ( keys %h ) { $h{$k} = () = grep { $_ eq $k } @a; # get count of matching elements }