in reply to Counting Random Elements
CSV of course immediately triggers "use a csv module", so that part is easy: Text::xSV or one of the CSV modules.
"most common occurence" should trigger "hash for uniqueness". Consider:
my @items = qw(foo poot bar foo baz poot foo); my %counts; ++$counts{$_} for @items; print "$_: $counts{$_}\n" for sort keys %counts;
The trick then is to figure out how to process groups coming at you a row at a time. There are a bundle of ways of doing that. A simple way is to perform the processing for the previous group each time you see a new group header (unless there was no previous group):
use strict; my @rowTypes = (1, 2, 3, 3, 3, 1, 2, 3, 3, 1, 2, 1, 3); my $count; for my $rowType (@rowTypes) { if ($rowType == 1) { # New group next unless defined $count; # No processing for first or empty + group print "$count "; $count = 0; # Reset count. Set to undef to skip empty group next; } ++$count if $rowType == 3; } print "$count " if defined $count; # Need to process the last group
|
|---|