Reputation: 377
I have an array which only has letters in it, no digits. I want to count the number of times a specific letter shows. I don't want to use a hash, i need to preserve the order of the list.
use strict;
use warnings;
my %counts;
$counts{$_}++ for @array;
print "$counts\n";
Upvotes: 0
Views: 584
Reputation: 69244
The code that you have seems to work find for counting the occurrences. The only problem you have is in displaying the counts. You're using a new scalar variable called $counts
which is undeclared and empty.
What you want is this:
use strict;
use warnings;
my %counts;
$counts{$_}++ for @array;
print "$_: $counts{$_}\n" for keys %counts;
Upvotes: 2