There are far better and shorter ways than this one, but I'm hoping this will allow you to see what's happening. If I understand you correctly, you want to extract the number in the input that has the highest 'count'. If so:

#!/usr/bin/perl use strict; use warnings; my @data = qw(2 3 3 3 5 7 8 12 32 44 55 12 3 23 43 33 1 4 25 43 42 1 4 + 5 3 3 3); my %seen; for my $num (@data){ $seen{$num}++; } my @keys = sort {$seen{$b} <=> $seen{$a}} keys %seen; my $highest = $keys[0]; my $count = $seen{$keys[0]}; print "Highest count: $highest, Count: $count\n";

It defines an idiomatic %seen hash for keeping track, and using each value in the data, sets the key as the number in data, and adds one to the current value. Then, we create a keys array with a backwards sort which populates the array with the data number that has the highest value (count) to lowest. We then set the count value to $count and the key (data number) to $highest.

If I misunderstood what you need, just clarify.

-stevieb


In reply to Re: Help fixing this piece of code by stevieb
in thread Help fixing this piece of code by perlynewby

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.