Welcome to Perl Monks.

The syntax problem with your code is that foreach runs a loop so you need to use it like this:

foreach (@vettore) { print $_ . "\n"; }

But it looks like you may actually want to print the output to your filehandle $fh by replacing your print with print $fh "$_\n"

Perhaps this script will help show some other ideas

# 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 s +ame print $out "$_ \n"; # Variables can be interpolated in a stri +ng } # end foreach loop } } close $in; close $out;

If you are going to post more often it may be worth having a look at how to format your posts :)

Cheers,
R.

Pereant, qui ante nos nostra dixerunt!

In reply to Re: 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.