http://qs1969.pair.com?node_id=135041


in reply to Re: Removing the ^M character, but also removing the newline
in thread Removing the ^M character, but also removing the newline

chomp is only going to remove the trailing newline character of the entire line. my issue is that i may have multiple new lines within my hash value, so chomp won't do it for me.

-c

  • Comment on Re: Re: Removing the ^M character, but also removing the newline

Replies are listed 'Best First'.
Re: Re: Re: Removing the ^M character, but also removing the newline
by steves (Curate) on Dec 30, 2001 at 03:01 UTC

    Tell chomp what your newline is like this:

    local $/ = "\r\n"; chomp;

    Chomp chomps whatever you tell it a newline is. local localizes that setting so it's automagically replaced with the standard newline for your system when the block the local setting is within is done.

    Name another language that makes stripping newlines this easy. 8-)

      Almost forgot. I wrote this simple dos_chomp function for cases where there may or may not be a CR (^M) before the LF. Enjoy!

      sub dos_chomp { if (!defined(@_[0])) { chomp; s/\r$//; } else { for (@_) { chomp; s/\r$//; } } return wantarray ? @_ : @_[0]; }