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 thisopen 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. } }
now this maybe is not exactly what you need to do but it will get you starteduse 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;
Cheers
UPDATE:
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: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;
also is there a space between > and 1? so you did several things wrong down there: it should look like this:>1 ATGT... >2 ATGGTGC..
and use the code tags like: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;
<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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |