Angharad,
What you want to use is a high water mark algorithm per item. It would look something like this:
#!/usr/bin/perl use strict; use warnings; my $input = $ARGV[0] or die "Usage: $0 <input>"; open(my $fh, '<', $input) or die "Unable to open '$input' for reading: + $!"; my %high; while (<$fh>) { chomp; my ($item, $group, $count) = split ' '; $high{$item} = $count if ! defined $high{$item} || $count > $high{ +$item}; } for my $item (keys %high) { print "$item high count was $high{$item}\n"; }

I have purposely left this code not working exactly as required (group is omitted) to give you something to work towards. If you need more help - remember that the entire line is stored in $_ Also, this assumes that counts will always be numeric and that they will be whitespace delimited. For something much more fancy, have a look at How A Function Becomes Higher Order

Update: You didn't mention what should happen if an item has more than one group with the same highest count. The algorithm can be modified to handle the first/last/all/random values in that situation but you will need to determine what is right for you.

Cheers - L~R


In reply to Re: Retrieving data with largest count by Limbic~Region
in thread Retrieving data with largest count by Angharad

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.