in reply to Re: How to add column into array from delimited tab file
in thread How to add column into array from delimited tab file

The structure you seem to be looking for is a 2-dimensional array - which, in perl, is an array of arrays:
my @aoa; # Array of arrays (2-d arrray) open my $CURINFILE, "<", $files[$i]" or die "Error couldn't open file +$files[$i]\n"; print "$files[$i]\n"; while(<$CURINFILE>) { chomp $_; push @aoa, [ split('\t')]; # Insert an array ref into the array +(which is what makes it 2-D) } close $CURINFILE; print "\nWriting output..."; #The first row of @aoa contains the titles, so skip that, and print th +e rest.... for my $row (@aoa[1..$#aoa]){ # That is a slice of the array, from in +dex 1 till the end print $row->[0]."\n"; # $row->[0] contains the contents of the fir +st column (ID) # Similarly, $row->[1] is the dataR1 column }

        What is the sound of Perl? Is it not the sound of a wall that people have stopped banging their heads against?
              -Larry Wall, 1992

Replies are listed 'Best First'.
Re^3: How to add column into array from delimited tab file
by hellohello1 (Sexton) on Feb 17, 2014 at 07:43 UTC
    Ok. when I put the $row into arrays, it doesn't print out anything?
    for my $row (@aoa[1..$#aoa]) { @ID = $row->[0]; @data = $row->[1..$#aoa]; } print $OUT1 "$_\n" for @ID; print $OUT2 "$_\n" for @data;
    the reason for array is so that I can calculate each value over the respective value in same array in a loop.
      I'm not clear on what or how you want to calculate - it seems that you want to get a single column out into an array - so - for example, if you wanted to get all dataR1, you could do:
      my @dataR1 = map { $_->[1] } @aoa[1..$#aoa]; #Loop over each DatR1: for my $dr1 (@dataR1){ # Calculate something using $dr1 }

              What is the sound of Perl? Is it not the sound of a wall that people have stopped banging their heads against?
                    -Larry Wall, 1992