From the description of your data, it sounds like you may be dealing with FASTA format. Even if you're not, the following technique (with a little modification) may do what you want.

#!/usr/bin/env perl use strict; use warnings; while (<DATA>) { if (/^>/) { print "\n" unless $. == 1; print; } else { chomp; print; } } print "\n"; __DATA__ >chunk1 c1_line1 c1_line2 c1_line3 >chunk2 c2_line1 c2_line2 c2_line3

Output:

>chunk1 c1_line1c1_line2c1_line3 >chunk2 c2_line1c2_line2c2_line3

Use Benchmark, to compare this with any other solutions, to determine which runs the fastest.

For the split and join (or .=) operations you mention, I'd have to guess you're using some form of 'local $/ = ...' (see perlvar: Variables related to filehandles) — I'd need more information to comment further on that. Of course, there's no reason why you can't compare those options with any others.

Two comments regarding stripping embedded newlines from a string using "s/\n//g/":

  1. That's incorrect syntax. There's no slash after the modifier (i.e. it's just s/\n//g). See perlop: Regexp Quote-Like Operators.
  2. Transliteration (e.g. y/\n//d) is probably faster. See perlop: Quote-Like Operators. [Note: y/// and tr/// are synonymous.]

If my guess at your requirements is wrong, please supply more information to help us help you. The guidelines in "How do I post a question effectively?" should point you in the right direction with respect to this.

-- Ken


In reply to Re: Print in loop or store and print once done by kcott
in thread Print in loop or store and print once done by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.