What about looping through the array with foreach(){} and incrementing either $smallboxes or $largeboxes depending on the value of the currently processed item?
Maybe you should give us some more infromation. Is the number of possible groups of elements fixed? If it's not you may need to do something like this:
foreach (@x) {
s/\d+$//; # strip the number
$counts{$_}++
}
foreach my $group (sort keys %counts) {
print "$group : $counts{$group} times\n";
}
That is first you strip the numbers to get the "name" of the group and then increase the group count in the %counts hash. Then you print all the groups.
Update: As arturo correctly points out I have a bug in the code. I'd modify the original array. The loop should look like this foreach (@x) {
my $group = $_;
$group =~ s/\d+$//; # strip the number
$counts{$group}++
}
Thanks arturo.
Jenda
Always code as if the guy who ends up maintaining your code
will be a violent psychopath who knows where you live.
-- Rick Osborne
Edit by castaway: Closed small tag in signature |