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

Replies are listed 'Best First'.
Re^3: concatenating multiple lines without using . operator
by Corion (Patriarch) 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 AnomalousMonk (Archbishop) 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.

Re^3: concatenating multiple lines without using . operator
by NetWallah (Canon) 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

      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

      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