you should really put your code in <code></code> tags. But from what I can see your code looks like this(more or less):
open in, "rep_set_ass_tax.fna"; while ($line=<in>) { if($line=~/>/) { @vettore=split(/\s+/, $line); open my $fh, ">>seq_id.txt" or die "Cannot open output.txt: $!"; # + There is no need to open a file each time you need to print somethin +g into it if it is the same file. just open it once before the loop a +nd print to it foreach (@vettore); # What is a loop and what is a proper way to w +rite one. So you started a loop and what are you going to do with it( +I mean variables in the loop) print $_ . "\n"; # OK, so you want to print something, but where + to STDOUT or into a file that you have opened close my $fh; # again my doesn't have any effect if use strict is +not called. } }
First of all try to use strict and warnings, you will get a lot of info about your errors. Second, you have a some syntax errors(see comments in the above code). I I were you I would do something like this
use strict; use warnings; open(IN, "<", "rep_set_ass_tax.fna") or die "Died!!"; open (OUT , ">","seq_id.txt") or die "Cannot open output.txt: $!"; while (my $line=<IN>) { if($line=~/>/) { my @vettore=split(" ", $line); foreach (@vettore){ print OUT $_ . "\n"; } } } close IN; close OUT;
now this maybe is not exactly what you need to do but it will get you started

Cheers

UPDATE:

use strict; use warnings; open(IN, "<", "rep_set_ass_tax.fna") or die "Died!!"; while (my $line=<IN>) { if($line=~/>(\d+)/) { print $1 . "\n"; } } close IN;
UPDATE 2: Did you manage to get it working?? Also is this a fasta file orreally a one line file "> 1 ATTTAAA..."??? it is a bit strange if it is a one liner. usually those files look like this:
>1 ATGT... >2 ATGGTGC..
also is there a space between > and 1? so you did several things wrong down there: it should look like this:
use strict; use warnings; my $infile = "rep_set_ass_tax.fna"; my $outfile = "seq_id.txt"; # you need to open this only if writing t +o a file open(IN, "<", $infile) or die "Died!!"; while (my $line=<IN>) { if($line=~/>\s*(\d+)/) { print $1 . "\n"; } } close IN;
and use the code tags like:

<code>
My code;
</code>

baxy


In reply to Re: writing array element to a file by baxy77bax
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.