Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Text file to a 2D array

by bjelli (Pilgrim)
on May 07, 2001 at 22:46 UTC ( #78603=note: print w/replies, xml ) Need Help??


in reply to Text file to a 2D array

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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://78603]
help
Chatterbox?
[Corion]: A good morning!
[Discipulus]: good morning Corion!
[Corion]: I spent parts of yesterday digging into Filter::Simple to make it not change the line numbers of unfiltered code, but I think I need regex help (which will come in a SoPW I guess). I tried @lines = /((?:"...."|[^" \n]+)\n)/gs, but that matched on...
[Corion]: ... foo "\0\0\0\n" bar\n by matching the first \n instead of matching the four-bytes-in- double-quotes part
[Corion]: ... and I still don't understand why ;)

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (9)
As of 2017-01-23 08:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you watch meteor showers?




    Results (191 votes). Check out past polls.