bazfaz has asked for the wisdom of the Perl Monks concerning the following question:

Bringing in multi-line text fields, I want to replace carriage returns with spaces, or remove them entirely. How?!
  • Comment on How do I remove carriage returns from scalars?

Replies are listed 'Best First'.
Re: How do I remove carriage returns from scalars?
by tachyon (Chancellor) on May 31, 2001 at 07:33 UTC

    Depending on platform, the \n sequence is converted by perl to:

    Unix: octal \012 hex 0xA dec 10 LF may be \n Dos: octal \015\012 hex 0xD0xA dec 13 10 CRLF may be \r\n Max: octal \015 hex 0xD dec 13 CR may be \r

    Although perl works for you trying to allow you to just use \n as your newline delimiter and let it sort the platform dependent details, many common *internet protocols* specify the \015\012 sequence and unfortunately the values of Perl's \n and \r are not reliable since they can and do vary across platforms.

    I suspect that $textfield is named from its HTML source so you will probably want to use a truly portable solution like this:

    $textfield =~ s/\015\012|\015|\012//g; If you prefer hex to octal :-) $textfield =~ s/\xD\xA|\xD|\xA//g;

    If you are confused by the \012 or \xA notation all this is saying to perl is what I want you to match is the ASCII char decimal 10 == octal 12 == hex A == binary 1010

    In expanded commented /x form: $textfield =~ s/ # substitute \015\012 # a CRLF sequence (DOS, MIME...) | # or \015 # a lone LF (mac) | # or \012 # a lone LF (unix) / # with literal '' /xg; # /x allow comments, /g do globally
    tachyon

      No, in DOS, "\n" is "\012". It only becomes "\015\012" when written to a file (and then only if that file handle is not in binmode -- and this is done by the C RTL, not by Perl). Even under Unix, "\n" is turned into "\015\012" when written to many devices (just not when written to files).

              - tye (but my friends call me "Tye")
Re: How do I remove carriage returns from scalars?
by myocom (Deacon) on May 31, 2001 at 05:20 UTC
Re: How do I remove carriage returns from scalars?
by kiz (Monk) on May 31, 2001 at 20:09 UTC

    I suspect that you want the Regular Expression Substitution command:

    $foo =~ s/\n/ /g;

    This will go through the whole of the string in $foo and globally ('cos of the trailing 'g') replace every \n with a space.

    If your files are coming from a mac or a PC, you will need to change the pattern to match on

    -- Ian Stuart
    A man depriving some poor village, somewhere, of a first-class idiot.