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.

Hi Kevin

You can indeed push onto your array, and what is more Perl will
automatically create an empty array if it doesn't already exist:

 push @{$subject{$ID}{$var}}, $var1;

You may also want to look at the built-in module List::Util to save
you writing code to calculate the total of the array's values.

HTH,

Rob

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


Reply via email to