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?!!"
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.