I am trying to process a text file in perl. I need to save data from a file to a database. The problem I am facing is that some fields contain a new line, which pushes me a bit. What would be the best way to hide these fields?
Example data.txt file:
ID|Title|Description|Date
1|Example 1|Example Description|10/11/2011
2|Example 2|A long example description
Which contains
a bunch of newlines|10/12/2011
3|Example 3|Short description|10/13/2011
Current (broken) Perl script (example):
use strict;
open (MYFILE, 'data.txt');
while (<MYFILE>) {
chomp;
my ($id, $title, $description, $date) = split(/\|/);
if ($id ne 'ID') {
$sqlInsert->execute($id, $title, $description, $date);
}
}
close (MYFILE);
As you can see from the example, in the case of ID 2 it is divided into several lines, causing errors when trying to refer to these variables undefined. How would you group them into the correct field?
Thanks in advance! (I hope the question was clear enough, it is difficult to determine the name)
source
share