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 disp开发者_如何转开发lays 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
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.
精彩评论