Hi,
I'd do it with a simple hash and no additional max-function, but here's a solution with your code. Basically, you need to count the number of groups and compare it with $max. Some other adjustments have been necessary, please see comments in the code:
#!/usr/bin/perl -w use strict; # always good unshift(@INC,"$ENV{'PWD'}"); use List::Util qw(max) ; # I could not find a "Util", but this one +seems fine ;-) my $file_name; my $ans; if (@ARGV == 1) { chomp ($file_name=$ARGV[0]); } else { print "\n\nPlease enter the file name to certify: "; chomp ($file_name=<STDIN>); while(1) { print "\n\nYou entered $file_name - is this correct? <y or n>:"; chomp ($ans=<STDIN>); if ($ans =~ /[Nn]/) { print "\n\nPlease enter the server name: "; chomp ($file_name=<STDIN>); next; } elsif ($ans =~ /[Yy]/) { last; } else { next; } } } open(STUFF,"<$file_name") or die "$!"; my %number_ids = () ; my $no_of_groups = 0; # used to count the number of groups of that +file while (my $line = <STUFF>) { $line =~ s/\s+\z// ; $line =~ s/\A\s+// ; my @csv = split(/:/, $line) ; if (defined($csv[1]) && ($csv[1] =~ m{((CO)\d{5})})) { my $num = $1; # my necessary due to strictures $number_ids{$num}++; $no_of_groups++; # increment Group Counter } elsif (defined($csv[0]) && ($csv[0] =~ m/^Group.*/i)) { show_most_popular(\%number_ids); print "$csv[1],"; %number_ids = (); } else { # what to 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), " appeared most and $max out of $n +o_of_groups times\n"; # output as requested ;-) } ;

Regards,
svenXY

In reply to Re: Count occurences of numbers by svenXY
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.