Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: writing array element to a file

by baxy77bax (Deacon)
on Apr 25, 2013 at 09:58 UTC ( [id://1030638]=note: print w/replies, xml ) Need Help??


in reply to writing array element to a file

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

Replies are listed 'Best First'.
Re^2: writing array element to a file
by francesca1987 (Initiate) on Apr 25, 2013 at 13:10 UTC
    Hi baxy! I did as you suggested in the update, the script run, but the output was empty! I run:
    use strict; use warnings; my $infile = "rep_set_ass_tax.fna"; my $outfile = "seq_id.txt"; open(IN, ">>", "rep_set_ass_tax.fna") or die "Died!!"; while (my $line=<IN>) { if($line=~/>(\d+)/) { print $1 . "\n"; } }
    What's the problem?
    close IN;
      This opens IN for output
      open(IN, ">>", "rep_set_ass_tax.fna") or die "Died!!";
      it should be
      open(IN, "<", "rep_set_ass_tax.fna") or die "Died!!";
      poj

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1030638]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-04-25 06:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found