# Not this... my @items = qw( banana orange banana apple ); my %counts = count_items( \@items ); print "$_ => $counts{$_}\n" for sort keys %counts; sub count_items { my @items = @{ $_[0] }; my %counts; for my $item (@items) { next unless defined $item && length $item; $counts{$item}++; } return %counts; } # But this... my $items = [qw( banana orange banana apple )]; my $counts = count_items( $items ); printf "%s => %s\n", $_, $counts->{$_} for sort keys $counts->%*; sub count_items ( $items ) { my $counts; for my $item ( $items->@* ) { next unless defined $item && length $item; $counts->{$item}++; } return $counts; }