Gossamer Forum
Home : General : Perl Programming :

Need help w/ name/value

Quote Reply
Need help w/ name/value
I have data in individual flat files. I'm stuck on a particular item and I've narrowed it down to how I"m assign & splitting name/values.

Sample flat file format (test.txt):

Code:
1,"Bob Jones"
2,"1313 Mockingbird Lane"
3,"Saturday is a good day to go hiking and playing ball and a bunch of other stuff"
4,"more stuff"

Part of script that reads file:

Code:
use warnings;


%FIELD = (); #clears the variable

open(AUDIT, "<test.txt") or die "Cannot find file - no such file or invalid path\n";

while (<AUDIT>) {
$Line = $_;
chomp($Line);
$Line =~ s/\"$//g; #strips off ending qoute mark (")
($LineNo, $LineValue) = split(/\,\"/,$Line); #splits pair by using delimiter of comma-quote (,")
$FIELD{$LineNo} = $LineValue;
}

### CHECK Length of Various Fields to Make sure they'll print ok
if (length($FIELD{'3'}) > 50) {
print "too many characters";
}

else {
print "done";
}

close(AUDIT);

This works... however if you add a "hard return" to line #3 in the flat file so that it reads:

Code:
3,"Saturday is a good
day to go hiking and playing ball and a bunch of other stuff"

then the script will see the $FIELD{'3'} variable as containing only "Saturday is a good" and therefore it "passes" the test.

So... it's gotta be the way I'm splitting and assigning the values (?)
Quote Reply
Re: [Watts] Need help w/ name/value In reply to
You're pulling data in line by line, so it's broken when it comes to multi-line data. Either use a proper CSV parsing module (eg. Text::CSV::Simple), or remove the new lines from the data before writing it to the file (ie. when test.txt is created).

Adrian
Quote Reply
Re: [brewt] Need help w/ name/value In reply to
Thanks... I'll peruse both options...