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

while(<DATA>){ s/\\$//; s/\\/<br>/; } __DATA__ <p> This affected me deeply because I was being treated for depression the +n. I felt she was replacing me as mother and grandmother.\ This scenario occurred as often as two to three times a week.\ Somehow, all of those present seemed to think I should be her "friend. +" Shouldn't I be allowed to choose whom I want to associate with? Now, add the fact that my sister and her husband are still very attached to my ex, as they should be.\ </p>
How to delete the last '\' and make all the other '\' to
. Now if the try to delete the '\' at the end, all the '\' are deleted. Please tell me how to go about it

Replies are listed 'Best First'.
Re: Paragraph break
by ikegami (Patriarch) on Sep 02, 2009 at 05:32 UTC

    Perl 5.10+:

    my $text = do { local $/; <DATA> }; $text =~ s/\A.*\K\\$//sm; $text =~ s/\\$/<br>/mg;

    Any version:

    my $text = do { local $/; <DATA> }; $text =~ s/(.*)\\$/$1/ms; $text =~ s/\\$/<br>/mg;
      #!/usr/bin/perl my $text; while (my $line = <DATA>){ $line =~ s/\\/<br>/; $text .= $line; } $text =~ s[<br>\s*<\/w*>][<\/w*>]s; print $text; __DATA__ <p> This affected me deeply because I was being treated for depression the +n. I felt she was replacing me as mother and grandmother.\ This scenario occurred as often as two to three times a week.\ Somehow, all of those present seemed to think I should be her "friend. +" Shouldn't I be allowed to choose whom I want to associate with? Now, add the fact that my sister and her husband are still very attached to my ex, as they should be.\ </p>
      Why does the script doesn't work. The expected output should be something similar as below
      <p> <br>This affected me deeply because I was being treated for depression + then. I felt she was replacing me as mother and grandmother.<br> This scenario occurred as often as two to three times a week.<br> Somehow, all of those present seemed to think I should be her "friend. +" Shouldn't I be allowed to choose whom I want to associate with? Now, add the fact that my sister and her husband are still very attached to my ex, as they should be.<br/> </p>
        It does work except for the fixing the last <br>. Revert the change you made and it will work.
Re: Paragraph break
by ashish.kvarma (Monk) on Sep 02, 2009 at 05:35 UTC
    First of all please preview before you submit any post.
    From the above post your problem is not very clear. What I get from it is that you want to replace all '\' with new lines (i.e. <br>) and remove the last one (just before the paragraph ends).
    If my guess is correct here is something to try
    my $text; while (my $line = <DATA>){ $line =~ s/\\/<br>/; $text .= $line; } $text =~ s[<br>\s*</p>][</p>]s; print $text; __DATA__ <p> This affected me deeply because I was being treated for depression the +n. I felt she was replacing me as mother and grandmother.\ This scenario occurred as often as two to three times a week.\ Somehow, all of those present seemed to think I should be her "friend. +" Shouldn't I be allowed to choose whom I want to associate with? Now, add the fact that my sister and her husband are still very attached to my ex, as they should be.\ </p>
    output looks like:
    <p> This affected me deeply because I was being treated for depression the +n. I felt she was replacing me as mother and grandmother.<br> This scenario occurred as often as two to three times a week.<br> Somehow, all of those present seemed to think I should be her "friend. +" Shouldn't I be allowed to choose whom I want to associate with? Now, add the fact that my sister and her husband are still very attached to my ex, as they should be.</p>
    Regards,
    Ashish