Mission has asked for the wisdom of the Perl Monks concerning the following question:
FYI: There are lots of different files, having different 'ammounts' of data seperated by commas. This file has six fields, but the next one could have only three fields, or over fifty fields, it doesn't matter.bob,100 maple st,1496570,AZ,3460702,yellow sue,2153 oak ave,1813820,HI,335802,green george,285 elm rd,156632,KY,397218,blue sally,113 plum st,144207,TX,305438,red rita,950 grove ct,2507570,OH,76427,orange
#!/usr/bin/perl -w use strict; my @restore; my @testArray = getData("test.txt"); print $testArray[0][0],"\n"; print $testArray[$#testArray][0],"\nGot it!"; sub getData { my $fileOpen = $_[0]; my ($tempData,$tempHold,@listTemp,@listDetail,@listFinal); open (DataIN,$fileOpen) || die "Darn."; while(<DataIN>) { $tempData = $_; $tempHold .= $tempData; } close DataIN; @listTemp = split(/\n/,$tempHold); for(my $i=0; $i<=$#listTemp; $i++) { @listDetail = @restore; @listDetail = split(/,/,$listTemp[$i]); for(my $j=0; $j<=$#listDetail; $j++) { $listFinal[$i][$j] = $listDetail[$j]; } } return @listFinal; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Text file to a 2D array
by merlyn (Sage) on May 07, 2001 at 18:39 UTC | |
|
Re: Text file to a 2D array
by suaveant (Parson) on May 07, 2001 at 18:41 UTC | |
|
Re: Text file to a 2D array
by bjelli (Pilgrim) on May 07, 2001 at 22:46 UTC | |
|
Re: Text file to a 2D array
by jeroenes (Priest) on May 10, 2001 at 19:47 UTC |