[Pdx-pm] sqlite and chomp

Steve Sandvik ssandv at gmail.com
Fri Apr 30 00:45:47 PDT 2010


sounds like your $/ eq "\n", not "\r\n", based on the behavior after
running the one-liner.
-----
>From the perldoc:

This safer version of "chop" removes any trailing string that
corresponds to the current value of $/  (also known as
$INPUT_RECORD_SEPARATOR in the English  module). It returns the total
number of characters removed from all its arguments. It's often used
to remove the newline from the end of an input record when you're
worried that the final record may be missing its newline. When in
paragraph mode ($/ = "" ), it removes all trailing newlines from the
string. When in slurp mode ($/ = undef ) or fixed-length record mode
($/  is a reference to an integer or the like, see perlvar) chomp()
won't remove anything.
-----
Steve Sandvik
On Thu, Apr 29, 2010 at 11:42 PM, Daniel Herrington
<dherrington at robertmarktech.com> wrote:
> All,
>
> I'm parsing through an array that was produced by slurping a file. One of
> the first things I do in the array is chomp the line to remove the newline
> character. I then do an insert into a sqlite table:
>
>    while (@caJIL) {
>        my $line = shift(@caJIL);
>        chomp($line);
>        my ($jc,$jv) = split (':',$line);
>        if ($jc =~ /insert_job/) {
>            $jv = substr $jv, 1;
>            my $sth = $dbh->prepare_cached("INSERT INTO atJobs (jobName)
> VALUES (?)");
>            $sth->execute($jv);
>        }
>    ....
>    }
>
> However, the chomp seems to be doing something funny to the line that sqlite
> doesn't like. Here is the output of a select on the table with the chomp:
> dan at blackswan:~/Documents/RobertMark/BofA/work/foo/VA2$ sqlite3 VA2-bofa.db
> SQLite version 3.5.9
> Enter ".help" for instructions
> sqlite> select * from atJobs;
> ||||||||||||||||||||||||||||||||||||||||||||||||||||
> sqlite> .quit
>
> And here is the output without the chomp:
> dan at blackswan:~/Documents/RobertMark/BofA/work/foo/VA2$ sqlite3 VA2-bofa.db
> SQLite version 3.5.9
> Enter ".help" for instructions
> sqlite> select * from atJobs;
> 1|WCM_WGCIB_MON_FW_1
> ||||||||||||||||||||||||||||||||||||||||||||||||||||
> sqlite> .quit
>
> I looked at the file in binary, and the line ends with a 0D 0A, which is
> CR/LF? If so, shouldn't perl simply look at it as a \n and move along? If I
> run the perl oneliner:
>
> perl -pi -e 's/\r\n/\n/g' file.txt
>
> against the file and then run my script, everything works fine. Isn't chomp
> supposed to remove the \r\n?
>
> thanks,
>
> --
> Daniel B. Herrington
> Director of Field Services
> Robert Mark Technologies
> dherrington at robertmarktechnologies.com
> o: 651-769-2574
> m: 503-358-8575
>
> _______________________________________________
> Pdx-pm-list mailing list
> Pdx-pm-list at pm.org
> http://mail.pm.org/mailman/listinfo/pdx-pm-list
>


More information about the Pdx-pm-list mailing list