in reply to Retrieving data with largest count

my %largest; while (<>) { my ($item, $group_id, $count) = split; next if $largest{$group_id} && $largest{$group_id}[2] >= $count; $largest{$group_id} = [ $item, $group_id, $count, $_ ]; } for (values(%largest)) { print($_->[-1]); }

Keeps first on ties.

Update: Added note at bottom.

Replies are listed 'Best First'.
Re^2: Retrieving data with largest count
by Limbic~Region (Chancellor) on Jun 02, 2009 at 15:25 UTC
    ikegami,
    I know that it is unlikely in this situation that there would be negative or even nil counts, but this code does the wrong thing in the following situation:
    ASDF 1 0 ASDF 1 -3
    Specifically, it first sets the high water mark to 0 and then to -3. (Originally, I didn't have the group the same - corrected)

    Cheers - L~R

      No it doesn't??? In:
      1ct9B 3 1 1f7uA 2 3 1gaxA 2 1 1gpmA 3 5 1ihoA 6 4 1mopA 6 3 1ileA 2 5 1iq0A 2 3 1vlhB 5 3 1jhdA 5 4 ASDF 1 0 ASDF 1 -3

      Out:

      1ihoA 6 4 ASDF 1 0 1gpmA 3 5 1ileA 2 5 1jhdA 5 4

      0 >= -3, so next is executed, so the record for -3 is ignored. The item with the highest value for group 1 (ASDF 1 0) is printed.

        ikegami,
        You replied to yourself and not me I didn't see the response initially. I must be having a second Monday. I read the following code:
        next if $largest{$group_id} && $largest{$group_id}[2] >= $count;
        as
        next if $largest{$group_id}[2] && $largest{$group_id}[2] >= $count;
        Obviously not having a computer at home for a month and a trip to Vegas to boot has shrunk what little brain cells I have.

        Cheers - L~R