eyepopslikeamosquito has asked for the wisdom of the Perl Monks concerning the following question:
A workmate asked me the best way to convert all " quoted strings that span more than one line into a string with the newlines and surrounding whitespace compressed to a single space. Note: he does not need to worry about escaped " within the " strings.
To clarify, this input data:
should produce this output:"boom" hello "" bill hello " " bill "baz hello jock" "boom2" abc "baz2 hello2 jock2 "
"boom" hello "" bill hello " " bill "baz hello jock" "boom2" abc "baz2 hello2 jock2 "
I suggested this code:
Though this code does appear to work, improvements or advice are welcome. Also, there may be diabolical test data that breaks my code that I have missed. Admittedly the spec is a bit vague, but if you see some test data that breaks the code above, please let me know.use strict; use warnings; my $s = <<"GROK"; "boom" hello "" bill hello " " bill "baz \t hello jock" "boom2" abc "baz2 hello2 \t jock2 " GROK $s =~ s{"([^"]*)"} { if ($1 =~ tr/\n//) { my $x = $1; $x =~ s/[ \t]*\n[ \t]*/ /g; '"' . $x . '"'; } else { '"' . $1 . '"'; } }eg;
Update: Added extra line hello " " bill to the test data to clarify the requirements. Thanks GrandFather. Also added extra space after jock2 to further clarify.
|
|---|