perl_seeker has asked for the wisdom of the Perl Monks concerning the following question:
I need to split the text on whitespace and store in an array @data, so that each row gets stored in one arraytest.txt 2004,21,22 2004,23,24 2004,25,26 . . . .
and so on for other rows.$data[0]=2004,21,22 $data[1]=2004,23,24
And then in another array say @arr1$data[0]=2004,21,22 $data[1]=2004,22,23
in another array @arr2$arr1[0]=2004 $arr1[1]=21 $arr1[2]=22
and so on for @arr3, @arr4( as many arrays as there are rows).Or is there a better way to do this?$arr2[0]=2004 $arr2[1]=23 $arr2[2]=24
The contents of @data are printing correctly in the while loop, but not in the for loop outside, so the contents ofopen DAT, 'test.txt' or die $!; my @data; my @row; while(<DAT>){ chomp; push @data,split; #print"\n$data[0]"; #print"\n$data[1]"; #print"\n$data[2]"; } $ldata=@data; #print"\n$ldata"; for($i=0;$i<$ldata;$i++){ #print"\nThe row:"; #print "\n$data[i]"; push @row,split(/,/,$data[i]); print"\nThe contents of the row:"; foreach $item(@row) { print "\nThe element:"; print "\n$item"; } } close DAT;
20040615 Edit by Corion: Changed title from 'Arrays'
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Splitting text into arrays
by Happy-the-monk (Canon) on Jun 12, 2004 at 08:31 UTC | |
by perl_seeker (Scribe) on Jun 13, 2004 at 07:51 UTC | |
by perl_seeker (Scribe) on Jun 13, 2004 at 07:51 UTC | |
|
Re: Splitting text into arrays
by Mr. Muskrat (Canon) on Jun 12, 2004 at 16:47 UTC | |
by perl_seeker (Scribe) on Jun 21, 2004 at 05:52 UTC | |
by davidj (Priest) on Jun 21, 2004 at 07:13 UTC | |
by perl_seeker (Scribe) on Jun 23, 2004 at 12:57 UTC | |
by perl_seeker (Scribe) on Jun 13, 2004 at 07:56 UTC |