So this should be an easy solution but for the life of me I can't figure out what I am doing wrong.

I've input a file in paragraph mode (ie set $/="") as much of the data i need to isolate is multi-line. I have set up a while (<>) loop, in which I have several if statements to pattern match. I am saving long text strings into a hash but I need the values not to have any embedded "/n", such as:

Justice Hudson did not participate in the consideration or decision of\n this case.

I need the entire sentence (ie the entire string) as a value in a hash with no embedded "\n". I can isolate the pattern fine, and I can assign this string to a hash value, but I can't get that value to read as a coherent single string. I tried the chomp function on the variable itself (properly, I think) but no luck; and when I try substituting I get a crazy result (and I have tried many variations) in which the substring that followed the \n is moved to the front of the string and superimposed over the beginning.

At its most basic, this is my script that produces this crazy result (in which the pattern from which to match and extract in the entire document is "JUDGES: Justice HUDSON did not participate in the consideration or decision of
this case.":

#!/usr/bin/perl -w my %case = (); # case hash my $judges = ''; # temp key open (IN, "/Users/MicWood/Documents/Perl/Opinion\ Scripts/output.txt") +; $/=""; # paragraph input mode while (<IN>) { if (/^JUDGES:\s(.+)\n\n/s){ $case{'judges'}=$1}; next;} print "before:\n"; print $case{'judges'}; $case{'judges'}=~ s/\n/ /g; print "\nafter:\n"; print $case{'judges'}; print "\n";

The result between the two command line prompts:

before: Justice HUDSON did not participate in the consideration or decision of this case. after: this case.SON did not participate in the consideration or decision of

Why is it superimposing the two parts of the string? Why when I substitute out the "\n" does it not just read as one long string like: Justice HUDSON did not participate in the consideration or decision of this case.

Thanks so much for your help! I know this is probably a simple issue but it is about to drive me crazy. Best, Michael


In reply to removing new line character embedded in multi-line string by micwood

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.