Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Removing empty line(s) with regex in a multiple strings in a variable

by monkfan (Curate)
on Sep 22, 2006 at 02:31 UTC ( #574298=perlquestion: print w/ replies, xml ) Need Help??
monkfan has asked for the wisdom of the Perl Monks concerning the following question:

Hi,
I have this string:
my $str = ' AAGTAATCAAGTATTACAAGAAACAAAAATTCAAGTAAATAACAGATAAT ATGTCAAAAGCTGTCGGTATTGATTTAGGTACAACATACTCGTGTGTTGC >YDR256C CTA1 969666 970216 GGGAAGAACTAAGAGATGTTATGGCTCGGAGAGTTTTGAAAAGCGAAATA GATTCGCTGCAAGTTTGTGAAGAAACCATCGACAAGAATTACAAGGTTAT '; # and it can be more than one empty lines
And I want to remove the empty line in between so that it gives this:
$VAR = ' AAGTAATCAAGTATTACAAGAAACAAAAATTCAAGTAAATAACAGATAAT ATGTCAAAAGCTGTCGGTATTGATTTAGGTACAACATACTCGTGTGTTGC >YDR256C CTA1 969666 970216 GGGAAGAACTAAGAGATGTTATGGCTCGGAGAGTTTTGAAAAGCGAAATA GATTCGCTGCAAGTTTGTGAAGAAACCATCGACAAGAATTACAAGGTTAT ';
Why my regex below doesn't work? What is the right solution?
use Data::Dumper; $str =~ s/[\s]+//mgx; print Dumper $str;

Regards,
Edward

Comment on Removing empty line(s) with regex in a multiple strings in a variable
Select or Download Code
Re: Removing empty line(s) with regex in a multiple strings in a variable
by GrandFather (Cardinal) on Sep 22, 2006 at 02:43 UTC

    Delete empty lines where empty includes white space only lines:

    $str =~ s/(^|\n)[\n\s]*/$1/g; print $str;

    Prints:

    AAGTAATCAAGTATTACAAGAAACAAAAATTCAAGTAAATAACAGATAAT ATGTCAAAAGCTGTCGGTATTGATTTAGGTACAACATACTCGTGTGTTGC >YDR256C CTA1 969666 970216 GGGAAGAACTAAGAGATGTTATGGCTCGGAGAGTTTTGAAAAGCGAAATA GATTCGCTGCAAGTTTGTGAAGAAACCATCGACAAGAATTACAAGGTTAT

    DWIM is Perl's answer to Gödel
Re: Removing empty line(s) with regex in a multiple strings in a variable
by ysth (Canon) on Sep 22, 2006 at 02:47 UTC
    Works for me, sort of. At least it gets rid of all the newlines leaving
    'AAGTAATCAAGTATTACAAGAAACAAAAATTCAAGTAAATAACAGATAATATGTCAAAAGCTGTCGGTA +TTGATTTAGGTACAACATACTCGTGTGTTGC>YDR256CCTA 1969666970216GGGAAGAACTAAGAGATGTTATGGCTCGGAGAGTTTTGAAAAGCGAAATAGATTCGC +TGCAAGTTTGTGAAGAAACCATCGACAAGAATTACAAGGTTAT';
    What are you seeing? If you just want to compress multiple newlines, you could use $str =~ y/\n//s; If there are space characters on the "blank" line, you'd want something like $str =~ s/^\s+//mg; (or $str =~ s/^\s+\n//mg; if there may be spaces at the start of a non-blank line).
Re: Removing empty line(s) with regex in a multiple strings in a variable
by jwkrahn (Monsignor) on Sep 22, 2006 at 03:04 UTC
    It looks like you want:
    $str =~ tr/\n//s;
      Does not work for me. But tr/\n//d or s/\n//g work.
Re: Removing empty line(s) with regex in a multiple strings in a variable
by graff (Chancellor) on Sep 22, 2006 at 03:13 UTC
    Your regex  s/[\s]+//mgx deletes all whitespace, and you don't want that -- you want to preserve the spaces that separate the four strings on the line that starts with ">".

    (I could also point out that in your regex, the square brackets and "m" and "x" modifiers could all be removed and it would do the same thing it does now -- including them has no effect at all in this case. Of course, this also means they do no harm, but if you don't understand why they do nothing in this case, it wouldn't hurt you to try reading the perlre manual page.)

    As GrandFather points out, you only want to eliminate the extra "\n" characters. The specific output that you say you want (with the initial "\n" still intact) would come out with just this:

    s/\n\s*/\n/g; # every string of \n plus 0-or-more whitespace --> sing +le \n
    (updated to emulate GrandFather's handling of "blank" lines that contain just spaces and/or tabs)
      I worked with the same trouble, and the only one good working way I found, it is $buffer =~ s/^\s*\n+//mg; Thank you!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://574298]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (11)
As of 2014-08-23 12:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (173 votes), past polls