Yes. That is, instead of all the code in the OP, you would want something like this:
#!/usr/bin/perl use strict; my %idcount; while (<>) { if ( /,CO(\d+)\s/ ) { $idcount{$1}++ } } my ( $highest ) = sort { $idcount{$b} <=> $idcount{$a} } keys %idcount +; my $total = 0; $total += $_ for ( values %idcount ); print "Highest count was for group CO$highest, with $idcount{$highest} + out of $total users\n";
For the sample data in the OP, that prints:
Highest count was for group CO12345, with 4 out of 6 users
(which I got by running the script in a terminal window, then pasting the 6 lines of data into the window, to emulate "typing" on stdin; the same result would happen by piping those six lines to the perl script from some other process, or storing them in a file and giving the file name as the sole command-line arg when running the script).

UPDATED to include declaring, computing and printing "$total" (as per the OP spec).

Oops... another update -- forgot about the case of possible ties for "highest". So, in place of the one line above that declares/assigns a value to $highest, you would need these two lines, which will set $highest to be a string of one or more comma-separated group-ids (including "CO" before the digits of non-initial id values):

my ($maxcount) = sort { $idcount{$b} <=> $idcount{$a} } keys %idcount; my $highest = join(",CO", grep { $idcount{$_} == $maxcount } keys %idc +ount );
Nothing else needs to change. Then replace "$idcount{$highest}" with "$maxcount" in the print statement.

In reply to Re^4: Count occurences of numbers by graff
in thread Count occurences of numbers 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.