Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: chomp() problems

by SparkeyG (Curate)
on Sep 24, 2002 at 17:31 UTC ( [id://200429]=note: print w/replies, xml ) Need Help??


in reply to chomp() problems

chomp will only remove the systems $/ ($INPUT_RECORD_SEPARATOR). In most Unix it is just \n, in DOS it's \r\n.
I guess you could fix the problem by:
{ local $/ = "\r\n"; chomp ($line); }
Edited to correct typo, and again edited to correct a typo and again edited to fix a type. A sick infant does wonders to your sleep habits, and therfore your typing skils ;) Note to self, do not post anything after cleaning up baby vomit. ;)

Replies are listed 'Best First'.
Re: Re: chomp() problems
by bart (Canon) on Sep 25, 2002 at 00:51 UTC
    In most Unix it is just \n, in DOS it's \r\n.
    No that's not correct. On DOS and Win32, it's also "\n". You see, the trick is that when reading from a text file, "\015\012" (AKA CRLF, "\r\n") is converted into a bare "\012" (LF, "\n"). Therefore, chomp() doesn't have to remove "\r\n", because there commonly will be, should be, only a bare "\n". And as chomp and $/ only use fixed strings not regexes for their workings, you can't have it both ways at the same time.

    And that, boys and girls, is why it doesn't work here. Access does store line endings as CRLF pairs. And that isn't very Perl compatible. Therefore, when reading data from Access in a Perl script, you should always turn CRLF into "\n", and vice versa when storing data back into Access.

Re: Re: chomp() problems
by fglock (Vicar) on Sep 24, 2002 at 17:56 UTC

    Oops. You mean $/ (INPUT_RECORD_SEPARATOR)
    instead of $\ (OUTPUT_RECORD_SEPARATOR)

    chomp works on $/

Re^2: chomp() problems
by Aristotle (Chancellor) on Sep 24, 2002 at 17:43 UTC
    local $\ = '\r\n';
    You did of course mean to use double quotes there.

    Makeshifts last the longest.

Re: Re: chomp() problems
by Wally Hartshorn (Hermit) on Sep 24, 2002 at 18:36 UTC

    One correction:

    s/in DOS it's \n\r/in DOS it's \r\n/

    Sure, blame it on the baby! :-)

    Wally Hartshorn

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://200429]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2024-03-19 08:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found