Re: Detecting line terminators in a CSV file



John W. Krahn wrote:

Because when on Windows the CR LF pair is converted to the "\n"
newline character.
perldoc PerlIO
[snip]
:crlf
A layer that implements DOS/Windows like CRLF line endings. On
read converts pairs of CR,LF to a single "\n" newline character.
On write converts each "\n" to a CR,LF pair. Note that this layer
likes to be one of its kind: it silently ignores attempts to be
pushed into the layer stack more than once.


I see. Well that explains it.

Are you sure that you want to "fix" this?


Certain. I need to find the correct line terminator, and then tell MySQL
what it is.

perldoc -f binmode


binmode is what I was after - thanks :)

# Parse the 1st line of the import file and extract fieldnames
eval{
open SOURCE, $options->{source}
|| die "Failed to open file $options->{source}.\nIs the file
already open?";
};

if ( $@ ) {
Gtk2::Ex::Dialogs::ErrorMsg->new_and_run(
title => "Error opening file!",
text => $@
);
return FALSE;
}


You don't *have* to die if open doesn't work! And besides, using the high
precedence || operator means it won't die even if you wanted it to.

open SOURCE, '<', $options->{source} or do {
Gtk2::Ex::Dialogs::ErrorMsg->new_and_run(
title => 'Error opening file!',
text => $!
);
return FALSE;
};


It's working on my system as-is. I'll check out that precedence thing
when I get some spare time.

if ( substr( $fieldnames, length( $fieldnames ) -2, 2 ) eq "\r\n" ) {


You don't have to call the length() function, you can just use a negative number:

if ( substr( $fieldnames, -2, 2 ) eq "\r\n" ) {


Now *that* is useful. Thanks :)

--
Daniel Kasak
IT Developer
NUS Consulting Group
Level 5, 77 Pacific Highway
North Sydney, NSW, Australia 2060
T: (+61) 2 9922-7676 / F: (+61) 2 9922 7989
email: dkasak@xxxxxxxxxxxxxxxxxxxx
website: http://www.nusconsulting.com.au
.



Relevant Pages

  • Re: Read/write with UCS-2* encodings - Possible???
    ... #Write Windows CRLF UTF-16 Files ... As far as I remember when I wrote that, I ended up getting a warning about wide characters from the:crlf layer, it doesn't happen anymore in perl5.10.0. ...
    (comp.lang.perl.misc)
  • Re: Read/write with UCS-2* encodings - Possible???
    ... And how one would easily switch:crlf layer off on such a handle? ... Doing just `binmode' switches off encoding as well; ... The wide-char encoding layer should notice that somebody wants to add ...
    (comp.lang.perl.misc)
  • Re: Wireless sensing function is missing?
    ... But I was actually using ralink card, which has 802.11 firmware implemented in open source. ... I am aware that freebsd has a 80211 layer as well. ... So before I dig into the source, I would appreciate it if anyone can let me know where CSMA is implemented, in the driver or net80211 provided by freebsd. ...
    (freebsd-net)
  • Re: How to change Perls concept of a newline in regexps?
    ... Maybe there exists a:cr layer for Mac-type text files? ... which is IMHO greatly superior to:crlf in all ...
    (comp.lang.perl.misc)