micwood has asked for the wisdom of the Perl Monks concerning the following question:
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: removing new line character embedded in multi-line string
by GrandFather (Saint) on Nov 01, 2007 at 23:54 UTC | |
|
Re: removing new line character embedded in multi-line string
by KurtSchwind (Chaplain) on Nov 02, 2007 at 00:20 UTC | |
|
Re: removing new line character embedded in multi-line string
by micwood (Acolyte) on Nov 02, 2007 at 02:26 UTC |