So you have a series of lines that look something like this

> 22 GATTGATGCC... > 2 GATGGATGTG... > 26 GATGCATGAT... > 52 GATGATGTGG...

And in your output file you just want the numbers.

The split we had in the original code splits each line on spaces into the @vettore array. If we only want to print the second element of this array (the number) then we do not need the foreach loop. We can alter our print to directly address the second element of the arrayprint $out "$vettore[1]\n"; (array indexing starts at 0). Here is our new line processing block:

while (my $line=<$in>) { if($line=~/^>/) { my @vettore=split(/\s+/, $line); print $out "$vettore[1]\n"; } }

For fun it can also be done as a one liner. Here I added a bit more checking of the line to ensure it had some GATC characters following the number

perl -nle "if (/^>\s+(\d+)\s+[GATC]+/) {print $1}" rep_set_ass_tax.fna >> seq_id.txt

If you are not on windows you may need to change the two " quotes to ' quotes.

Cheers,
R.

Pereant, qui ante nos nostra dixerunt!

In reply to Re^3: writing array element to a file by Random_Walk
in thread writing array element to a file by francesca1987

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.