Mission has asked for the wisdom of the Perl Monks concerning the following question:
Before anyone reads further, please let me explain. I'm trying to expand my Perl knowledge. I have an answer to this problem, but I'd like a different approach to this issue. Hopefully this will help others, as I'm sure it will help myself. TIA!
I have a variety of text files that are set up in the same manner. They are comma delimited files, with one 'row' of data seperated by "\n". I need to read in any of these files, and put them into a 2D array, which I return. So I'm passing the file name to open, and returning the 2D array.Here is a sample data file called "text.txt":
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
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.
So here is my code: (Remember, this works. I'm looking for a better way to do it!)
#!/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;
}
Since I'm using two scalars and three arrays to get the data, I'm sure I should be doing something different. I've looked at other people's code, but I just don't see anything that might work. As a warning, I do not use regex (yet), so if you post one, I'll probably not understand (yet).TIA
- Mission "Heck I don't know how to do it either, but do you think that's going to stop me?!!"
Re: Text file to a 2D array
by merlyn (Sage) on May 07, 2001 at 18:39 UTC
|
my @final = map { chomp; [split /,/] } <INFILE>;
-- Randal L. Schwartz, Perl hacker | [reply] [Watch: Dir/Any] [d/l] |
Re: Text file to a 2D array
by suaveant (Parson) on May 07, 2001 at 18:41 UTC
|
my @testarray;
open(DATA, 'test.txt') or die $!;
while(<DATA>) {
chomp; #remove trailing \n
push @testarray, [split ',', $_]; #[] makes anonymous
# array with values from split, pushed on in order...
# could use unshift if you wanted them reversed
}
close DATA;
That's about it... pretty straightforward
Update Damn that Merlyn... :) Well... mine has more comments :)
- Ant | [reply] [Watch: Dir/Any] [d/l] |
Re: Text file to a 2D array
by bjelli (Pilgrim) on May 07, 2001 at 22:46 UTC
|
I've tried to maintain your
variable names, and aimed more for
understandability than brevity:
while ($tempData = <DataIN>) {
chomp($tempData);
$listTemp = [];
@$listTemp = split(/,/ , $tempData);
push (@listFinal, $listTemp);
}
return @listFinal;
Read one line at a time, the text of the current
line is stored in $tempData. Remove the trainling newline
from $tempData.
Generate a new, empty anonymous array, $listTemp
is a reference to that array.
Split the line up at the commas, this gives
you a list of values. Those are stored in the Array
that $listTemp points to.
Now, push $listTemp into the resulting Array.
--
Brigitte 'I never met a chocolate I didnt like' Jellinek
http://www.horus.com/~bjelli/ http://perlwelt.horus.at | [reply] [Watch: Dir/Any] [d/l] |
Re: Text file to a 2D array
by jeroenes (Priest) on May 10, 2001 at 19:47 UTC
|
I'm late, but I just can't leave this huge plugging oppertunity.
Try Supersplit! (also at cpan)
use SuperSplit;
$list = supersplit_open(',' ,'test.txt');
"We are not alone"(FZ) | [reply] [Watch: Dir/Any] [d/l] |
|
|