OK, so you need to identify the start and end of each group, and count the number of times within each group that each number occurs, and print out the most popular ?
What do you want to do if several are equally popular ?
Are those blank lines before each group header, or blank columns at the start of each group header ?You want to read the file, line by line. Unless the CSV is guaranteed simple, you should consider using CSV module to parse it for you. Anything involving recognising strings, probably you want to use a hash for. (With numbers you could use an array, but if the number range is bit or sparse, use a hash anyway.) Then unpeel the hash to extract the collected data.
So... something along the lines of:
appears to do what you asked for.use strict ; use warnings ; use List::Util qw(max) ; open my $STUFF, "<", "stuff.csv" or die "$!" ; my %number_ids = () ; while (my $line = <$STUFF>) { $line =~ s/\s+\z// ; $line =~ s/\A\s+// ; my @csv = split(/\s*\,\s*/, $line) ; if (defined($csv[2]) && ($csv[2] =~ m/^\d+$/)) { $number_ids{$csv[2]}++ ; } elsif (defined($csv[4]) && ($csv[4] =~ m/^group\:/i)) { show_most_popular(\%number_ids) ; print $line, "\n" ; %number_ids = () ; } else { # OK, so what do we do with peculiar lines ? } ; } ; show_most_popular(\%number_ids) ; sub show_most_popular { my ($r_ids) = @_ ; return if !%$r_ids ; my $max = max(values %$r_ids) ; my @popular = () ; while (my ($id, $count) = each %$r_ids) { if ($count == $max) { push @popular, $id ; } ; } ; print join(',', sort @popular), "\n" ; } ;
Various bits of Perl magic:
OK, so I was at a loose end.
Update: I should also have noted that the code is assuming that the 'Group:' lines include the apparently floating ',,,,'. Also, that when knocking out this kind of thing -- scan a file to recognise and pick out stuff to process -- it is important to trap stuff that hasn't been recognised; the odd's are that you'll find bugs either in the code or in your understanding of the data.
In reply to Re: Counting Random Elements
by gone2015
in thread Counting Random Elements
by walkingthecow
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |