Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: chomp() problems

by charnos (Friar)
on Sep 24, 2002 at 17:52 UTC ( [id://200443]=note: print w/replies, xml ) Need Help??


in reply to chomp() problems

$line =~ tr/\r\n//d; should do what BrowserUK suggested..IIRC, the /d is required to delete characters. Checking the value of $/ would help ascertain why chomp() isn't working the way you expected.

Also, $line =~ s/\r\n$//; would probably more accurately replicate chomp()'s functionality.

Update: Thanks to bart, who reminded me that the $INPUT_RECORD_SEPARATOR is $/, not $\. There still appear to be quite a few incorrect vars floating around this thread.

Replies are listed 'Best First'.
Re: Re: chomp() problems
by paulbort (Hermit) on Sep 25, 2002 at 21:06 UTC
    I just ran into this recently with files being FTP'd from Windows to Linux, and I have to agree with Flexx: \r and \n are system-specific, where this is a byte-specific problem. What I used was similar to charnos' substitution, but fixed to ASCII:
    $line =~ s/\xD|\xA//g;

    This removes all stray CR/LF from any ASCII to any ASCII. (Sorry, no EBCDIC support.) As long as Perl can figure out where the line breaks are, this will get rid of the odd bits.

    --
    Spring: Forces, Coiled Again!

      Yup. Some like it hex... ;)

      Update: Actually, what you wrote is EBCDIC compatible. It'll substitute DOS CRLF's on any system, be it an ASCII or an EBCDIC one. You're using discrete ordinals (hexadecimal ones, in your case) instead of the infamous logical symbols \r\n, and that's what makes your statement portable.

      So long,
      Flexx

      PS: Someone downvoted this node (it's -1 by the time of this writing). Why? What did I do wrong here?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (1)
As of 2024-04-25 19:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found