I would use something similar to what swiftone has written, but I would populate the hash directly and skip the array:
while (<STDIN>) {
$some_hash{$_}++;
}
The value of each key is the numer of entries in the list. If you need to iterate over the list, you use something like this:
foreach (keys %some_hash) {
...some code here...
}
If you maintain a seperate list and hash, you have to update both if one changes. With this method, you only need to track one and thus have an easier maintenance job. The drawback is you lose ordering on the list, but since we have user entered data, this ordering may not matter. If necessary, you could try to keep order with the following (untested) code:
my $position = 1;
while (<STDIN>) {
$myhash{$_}{'item'}++;
$myhash{$_}{'position'} = $position++ if ! exists $myhash{$_}{'pos
+ition'};
}
Cheers,
Ovid
Join the Perlmonks Setiathome Group or just go the the link and check out our stats. |