views:

139

answers:

1

I am just starting with perl and would like some help with arrays please. I am reading lines from a data file and splitting the line into fields:

open (INFILE, $infile);
do {
my $linedata = <INFILE>;
my @data= split ',',$linedata;
....
} until eof;

I then want to store the individual field values (in @data) in and array so that the array looks like the input data file ie, the first "row" of the array contains the first line of data from INFILE etc.

Each line of data from the infile contains 4 values, x,y,z and w and once the data are all in the array, I have to pass the array into another program which reads the x,y,z,w and displays the w value on a screen at the point determined by the x,y,z value. I can not pas the data to the other program on a row-by-row basis as the program expects the data to in a 2d matrtix format. Any help greatly appreciated. Chris

+3  A: 

That's not really that difficult, you just need to store the splits, not in their own separate list, but in an array, taking up a slot of a larger array:

my @all_data;

while (my $linedata = <INFILE>) { 
   push # creates the next (n) slot(s) in an array
       @all_data
     , [ split ',',$linedata ] 
       # ^ we're pushing an *array* not just additional elements.
      ; 
}

However, if you're just trying to read a commonly-known concept as a comma-separated values format, then have a look at something like Text::CSV, because the full capabilities of CSV is more than splitting on commas.

Axeman
Thanks for your speedy replyAxeman, will give it a go
Chris
These solutions work well but the problem I now have is how do I get the actual values out of the array? eg if the input file is 1,2,3,45,6,7,8I want to te be able to pass those values to another program from the arrayThankschris
Chris
To give a litle more detail, the code that reads the data is RunMenu( "FILES_ASCII_READ", "abort", sub { PanelResults( "spec_choice", "data_file" => $csvfile,....$csfile is the text file containing the data 1,2,3,45,6,7,8....and this is read in ok.I would like to substiute the data array for $csvfile
Chris