I want to write the complete header line (starting with '>') in my output files.  Can I use the split function ...?

Sure you can use the split function, but if you just want to print the header line as is (i.e. copy it from the input), you wouldn't need to split up the record. As you have it, the header would be printed already without further ado.  Think of it this way: $_ holds an entire record, with the leading '>' removed (to more easily handle the edge cases that result from the way the input is being split by $/).  For example, for the first record, $_ would hold the string (including the newlines)

"aw1.a1 bhi|tn|56564 pairs:40098 ATGCTAGATGCTAGCTAGCTAGCACTGAT CGATGCTAGCGTAGTCAGCTGATGCTGTA CGATGCTAGTCGTACG "

You can do with it whatever you like before you print it out, e.g. take it apart using split or via regex captures, perform regex substitutions on it, etc.


Some more notes: The key for the hash is extracted via regex capture

my ($name) = /^(\w+)/;

which would extract "aw1" in this case, because \w+ stops matching at the dot. In case you'd need to extract keys as "aw1.a1" (or some such — I'm no fasta expert), you could modify the regex to also capture the dots

my ($name) = /^([\w.]+)/;

or up until the first whitespace char in the line

my ($name) = /^(\S+)/;

Or in case you'd want to print the headers only (which I'm not quite sure from your description), you could extract it similarly with a regex

my ($header) = /^([^\n]+)/;

or by splitting on newlines

my ($header) = split /\n/;

And so on...
Hope this gives you some starting points to tailor it to your specific requirements.


In reply to Re^5: Compare hash with arrays and print by almut
in thread Compare hash with arrays and print by ad23

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.