On Wed, 14 Nov 2001 12:42:44 -0800, ehughes wrote:

>I have some simple files to parse. The data looks like this:
>
>54            6342059
>80            6342059
>31            6158566
....

>The first column is an activity code and the second column is a client id
>number.
>
>Here is my task. I need to find the number of unique individuals that
>participated in each activity. Now the boring, tedious way would be to write
>something like this:
>
>for my $row {
>   $act_54{$row1} if (column1 == "54");
>   $act_80{$row1} if (column1 == "80");
>   $act_90{$row1} if (column1 == "90");
>}
>print scalar (keys %act_54);
>print scalar (keys %act_80);
>print scalar (keys %act_90);

This looks like fake code to me.
        
>Surely, there is something that will capture the data a little easier than
>defining a variable for each activity. There are more than 30 activities.

Why don't you use a hash of hashes? Assuming that $activity can be
something like "54" and $client like "6342059", this may do:

        while(<>) {
            chomp;
            my($activity, $client) = split /\s+/;
            $act{$activity}{$client}++
        }
        local($\, $,) = ("\n", " ");
        foreach my $activity (sort { $a <=> $b} keys %act) {
              print "$activity:", sort keys %{$act{$activity}};
        }

-- 
        Bart.

Reply via email to