Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
laziness, impatience, and hubris
 
PerlMonks  

Re^2: concatenating multiple lines without using . operator

by anonym (Acolyte)
on Jun 13, 2012 at 13:31 UTC ( #975978=note: print w/ replies, xml ) Need Help??


in reply to Re: concatenating multiple lines without using . operator
in thread concatenating multiple lines without using . operator

Thanks.Yep, I tried $_ =~ s/\r\n//g; $seq{$chr} = join ("", $_); already but it does nt concatenate as expected .Below is my code:

while (<IN>) { chomp; if (/^>chr(\S*)$/) { $chr = $1; #print STDERR "[$chr]\n"; } else { chomp $_; $_ =~ s/\s\r\n\t//g; $seq{$chr} = join ("",$_); #$seq{$chr} = `perl -pe 'chomp; END {print "\n" }' $file`; #$seq{$chr} .= $_; print "$seq{$chr}\n"; } #print OUT "$seq{$chr}\n"; }

Thanks


Comment on Re^2: concatenating multiple lines without using . operator
Download Code
Re^3: concatenating multiple lines without using . operator
by Corion (Pope) on Jun 13, 2012 at 13:36 UTC

    Why didn't you show this code when you asked your initial question?

    Please also explain what this regular expression in your code is supposed to do:

    $_ =~ s/\s\r\n\t//g;

    See perlre and YAPE::Regex::Explain.

    Q:\>perl -MYAPE::Regex::Explain -we "print for YAPE::Regex::Explain->n +ew(shift)->explain;" "\s\r\n\t" The regular expression: (?-imsx:\s\r\n\t) matches as follows: NODE EXPLANATION ---------------------------------------------------------------------- (?-imsx: group, but do not capture (case-sensitive) (with ^ and $ matching normally) (with . not matching \n) (matching whitespace and # normally): ---------------------------------------------------------------------- \s whitespace (\n, \r, \t, \f, and " ") ---------------------------------------------------------------------- \r '\r' (carriage return) ---------------------------------------------------------------------- \n '\n' (newline) ---------------------------------------------------------------------- \t '\t' (tab) ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------
Re^3: concatenating multiple lines without using . operator
by NetWallah (Monsignor) on Jun 13, 2012 at 13:45 UTC
    It seems that what you are looking for is the "s" modifier for regex. Try:
    $_ =~ s/[\s\r\n\t]//sg;
    Also note - since the \s \r .. etc are alternative characters, and NOT a sequence, I have placed them in [brackets].

    From the docs:

    s     Treat string as single line. That is, change "." to match any character whatsoever, even a newline, which normally it would not match.
    Update: Ignore the suggestion to use the "s" modifier. It is not necessary. See jwkrahn note below.

                 I hope life isn't a big joke, because I don't get it.
                       -SNL

      Thanks for the reply.I tried this but it is still printing in the same format. I am not sure where I am going wrong.

        So - you want to SLURP and CONCATNATE. Try replacing your entire WHILE loop with:
        do {local $/; ($seq{$chr}=<IN>)=~s/[\s\r\n\t]//g};

                     I hope life isn't a big joke, because I don't get it.
                           -SNL

      what you are looking for is the "s" modifier
      ...
      change "." to match any character

      The pattern used does not contain the "." character class so why would the "s" modifier be needed?

        You are right - it is not necessary, and has no effect in that context.

        The node has been updated to reflect that, and acknowledge your catch. Thanks.

                     I hope life isn't a big joke, because I don't get it.
                           -SNL

Re^3: concatenating multiple lines without using . operator
by AnomalousMonk (Prior) on Jun 13, 2012 at 14:37 UTC
    $seq{$chr} = join ("",$_);

    The join Perl built-in joins a list of strings. In the statement above, the list consists in the single string contained in the  $_ scalar, so the output of  join will be exactly the same as the input.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2013-05-23 03:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best material for plates (tableware) is:









    Results (473 votes), past polls