in reply to Retrieving data with largest count

Please post your code the next time, often a few small changes suffice to make such a program work.

... my %best; while (my $line=<YOURFILE>) { chomp $line; my ($item,$group,count)= split(/\s+/,$line); if ((not exists $best{$group}) or $best{$group}{count}<$count) { $best{$group}{count}=$count; $best{$group}{item}= $item; } } #to print them out foreach my $group ( keys %best) { print "$best{$group}{item} $group $best{$group}{count}\n"; }

PS: You didn't specify what should happen when two items in a group have the same count.

Replies are listed 'Best First'.
Re^2: Retrieving data with largest count
by Angharad (Pilgrim) on Jun 02, 2009 at 15:15 UTC
    Many thanks for the advice so far. Much appreciated. In answer to your question, if two items have the same count, choosing an item at random is fine.
      "choosing an item at random is fine"

      and also the hardest option. All candidates in a group have to be retained until processing the input data is complete, then all groups have to be checked for multiple candidate items and a random item selected.

      If either 'first' or 'last' is also fine then either of those is easier to implement.


      True laziness is hard work