Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Text file to a 2D array

by Mission (Hermit)
on May 07, 2001 at 18:36 UTC ( [id://78508]=perlquestion: print w/replies, xml ) Need Help??

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?!!"

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
    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
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
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)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://78508]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2024-03-29 04:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found