#!/usr/bin/perl -w
use strict;
use Data::Dumper;
my @ID = (1,1,1,1,1,1,1,1,2,2,2,2,2,2,3,3,4,4,4,4,4,5,5,5,5);
#
# count
#
my %items =();
$items{$_}++ for @ID;
print "$_ => $items{$_}\n" for sort keys %items;
#
# detail
#
%items =();
my $count =0;
push @{$items{$_}}, $count++ for @ID;
print Data::Dumper->Dump([ \%items], ['items']);
__END__
output:
1 => 8
2 => 6
3 => 2
4 => 5
5 => 4
$items = {
'4' => [
16,
17,
18,
19,
20
],
'1' => [
0,
1,
2,
3,
4,
5,
6,
7
],
'3' => [
14,
15
],
'2' => [
8,
9,
10,
11,
12,
13
],
'5' => [
21,
22,
23,
24
]
};
(update) Instead of Data::Dumper, you could also get your details this way:
print "$_ => (@{$items{$_}})\n" for sort keys %items ;
__END__
1 => (0 1 2 3 4 5 6 7)
2 => (8 9 10 11 12 13)
3 => (14 15)
4 => (16 17 18 19 20)
5 => (21 22 23 24)
See also Perl Idioms Explained - keys %{{map{$_=>1}@list}} in Tutorials to see how the hash mechanism works.
As for the error in your code, this node could be of help.
|