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

Hi, I have tried putting print $OUT1 "$_\n" for @data1; as what netwallah suggested. It is able to print the output I want. But in the command line, it shows:
Processing data... Original.txt Writing output...Use of uninitialized value $metabolite[44] in string +at G:\Metabolomics\Programming\PERL\Ratio Test\ratio test (update +d 12 feb 1 4).pl line 101, <CURINFILE> line 1 (#1) (W uninitialized) An undefined value was used as if it were alread +y defined. It was interpreted as a "" or a 0, but maybe it was a mi +stake. To suppress this warning assign a defined value to your variables. To help you figure out what was undefined, perl will try to tell y +ou the name of the variable (if any) that was undefined. In some cases it + cannot do this, so it also tells you what operation you used the undefine +d value in. Note, however, that perl optimizes your program and the opera +tion displayed in the warning may not necessarily appear literally in y +our program. For example, "that $foo" is usually optimized into "that + " . $foo, and the warning will refer to the concatenation (.) operat +or, even though there is no . in your program. Writing output...Uncaught exception from user code: Error couldn't create new Directory at G:\Metabolomics\Program +ming\PERL\ Ratio Test\ratio test (updated 12 feb 14).pl line 98, <CURINFILE> line + 2. at G:\Metabolomics\Programming\PERL\Ratio Test\ratio test (updated 12 + feb 14).p l line 98 Press any key to continue . . .
is it some kind of error? In addition, how do I actually loop from dataR1 to dataR3 to put into different arrays instead of having to type out manually as different files has different number of dataRx. I know it has something to do with this line: =~ /_data(\d+)R/ somewhere in the code but I have no idea how.
push(@data1, $columns[1]); push(@data2, $columns[2]); push(@data3, $columns[3]);
into something like this after finding the /_dataS(\d+)R/ in the column headers:
push (@data[j], $columns[j]);
I appreciate if there is any link related to that that can push me to the right direction. :) Thanks for the help by the way!

Replies are listed 'Best First'.
Re^2: How to add column into array from delimited tab file
by NetWallah (Canon) on Feb 17, 2014 at 07:04 UTC
    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

      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