On Monday 29 October 2007 09:15, Kevin Viel wrote:
> I have a dataset of values for variables for subjects.  Some subject
> have multiple records, requiring an average:
>
> ID  Var1 Var2
> 1   1.0  2.0
> 1   2.0  1.5
>
> -------------
> 1   1.5  1.75
>
> If I collect the variables into an array, I can average the contents
> after I have processed the entire file.  It would seem that I want
> something like:
>
> my %subjects ;
>
> $subjects{ $ID }{ $var }[ 0 ] = $var1 ;
> $subjects{ $ID }{ $var }[ 0 ] = $var2 ;
>
> It does not seem like I could PUSH values onto the array:
>
> push $subject{ $ID }{ $var } , $var1 ;
>
> My first inclination is to use EXISTS and a create a variable
> intended to INDEX the array:
>
> my $index ;
> if ( ~ exists $subjects{ $ID }{ $var } ) {
>   $index = 0 ;
> } else { $index = $subjects{ $ID }{ $var } )
>
>   $subjects{ $ID }{ $var }[ $index ] = $var1 ;
>
>
> I would appreciate any comments or suggestions concerning this
> approach.

It looks like you could do something like this:


my %subjects;

while ( <FILE> ) {
    next unless /^\d/;
    my ( $ID, @vars ) = split;
    $subjects{ $ID }{ count }++;
    for my $index ( 0 .. $#vars ) {
        $subjects{ $ID }{ sum }[ $index ] += $vars[ $index ];
        }
    }

# post-processing averages
for my $ID ( sort { $a <=> $b } keys %subjects ) {
    for my $var ( @{ $subjects{ $ID }{ sum } } ) {
        $var /= $subjects{ $ID }{ count };
        }
    print join "\t", $ID, @{ $subjects{ $ID }{ sum } };
    }




John
-- 
use Perl;
program
fulfillment


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to