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:

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" ; } ;
appears to do what you asked for.

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.