in reply to still confused with CGI and carriage returns carriage returns

(Okay I wrote then when I must have been on crack, its wrong, so see the replies ...)

Original text:
Your problem is the$description =~ tr/\n/ /; You should use: $description =~ s/\n/ /g; I think tr/// treats the "\n" liternally as a "\" and "n", while the s/// will treat it as a metacharacter.

Replies are listed 'Best First'.
RE: Re: still confused with CGI and carriage returns carriage returns
by chromatic (Archbishop) on Apr 26, 2000 at 19:31 UTC
    It works for me:
    my $string = "hi, \n\nhow are you\n?"; $string =~ tr/\n/X/; print ">>$string<<\n";
    Result: >>hi, XXhow are youX?<<
      Okay so I am an ass yet again. I did try it on my system before I rambled and the tr did not work, so I must have had a dumb little typo.

      Just for kicks I benchmarked the s/// vs tr/// since they seem to do the same in this reguard:
      use Benchmark; $str = "\n#\n#\nSTUFF\n#\n#\n"; timethese(1000000, { 's' => sub { $a = $str; $a=~s/\n/ /g; }, 'tr' => sub { $a = $str; $a=~tr/\n/ /; } }); RESULTS: Benchmark: timing 1000000 iterations of s, tr... s: 11 wallclock secs (11.22 usr + 0.00 sys = 11.22 CPU) tr: 3 wallclock secs ( 3.61 usr + 0.00 sys = 3.61 CPU)
      The moral of the story: Use tr/// where you can.