in reply to Retrieving data with largest count

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

Replies are listed 'Best First'.
Re^2: Retrieving data with largest count
by ikegami (Patriarch) on Jun 02, 2009 at 15:49 UTC

    I have purposely left this code not working exactly as required (group is omitted) to give you something to work towards.

    Also, your code finds the highest count per item, not per group. Since each item probably only appears once, you're just printing out the input.

      ikegami,
      Yes, (group is omitted) means I didn't consider group which is what the OP asked for. The was intentional as indicated in the same comment. I originally misread the question and when I discovered my output didn't match the OPs (before posting), I decided to leave it broken. Teach a man to fish so to speak. I guess I should have made that more clear.

      Cheers - L~R