# The following two lines check your code and # alert you to many common errors use strict; use warnings; # Lets keep the filespec out the main code # using my when defining a variable limits its scope. my $infile = "rep_set_ass_tax.fna"; my $outfile = "seq_id.txt"; # No point going on if we can not open both files # You were opening the outfile in a loop before, lots of opens # and closes happening that were not needed open my $in, '<', $infile or die "Can't read $infile: $!\n"; open my $out, '>>', $outfile or die "Can't read $outfile: $!\n"; while (my $line=<$in>) { if($line=~/^>/) { # ^ added for only lines starting with > my @vettore=split(/\s+/, $line); foreach (@vettore) { # can use for(@array) but they are the same print $out "$_ \n"; # Variables can be interpolated in a string } # end foreach loop } } close $in; close $out;