Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

How can i print to my outfile handle?

by lomSpace (Scribe)
on Feb 20, 2011 at 21:27 UTC ( [id://889248]=perlquestion: print w/replies, xml ) Need Help??

lomSpace has asked for the wisdom of the Perl Monks concerning the following question:

hello,
I need to print each element of an array to a file, but I am experiencing
problems with printing to an outfile handle. My goal is to print each array
element to a file in a directory, my desktop.
I keep getting the error that my filehandle is closed. Any direction on how
to get around this would be greatly appreciated!
Here is my code:
#!/usr/bin/perl -w use strict; local $/ = '//'; my $genpept = "/Users/mb/Desktop/proteins.gp"; my $peptstr1 = '/Users/mgavibrathwaite/Desktop/'; open(my OUT,">$peptstr1") or die $!; open(my IN,$genpept); #my $i = 1; while (my $records = <IN>){ #$i++; chomp $records; my @genpept; push @genpept,($records); print"Processed infile record: $.\n"; foreach(@genpept){ print "$_"; } } close(IN); close(OUT);

LomSpace

Replies are listed 'Best First'.
Re: How can i print to my outfile handle?
by Eliya (Vicar) on Feb 20, 2011 at 23:23 UTC

    I can see three errors:

    • open(my OUT, ... should be open(my $OUT, ...  Or open(OUT, ....  Same for IN.

    • Your file name to open for output is a directory  ('/Users/mgavibrathwaite/Desktop/').  That doesn't work.

    • You're printing to STDOUT instead of to the $OUT file handle you tried to open.  That would be print $OUT "foo\n";

Re: How can i print to my outfile handle?
by jwkrahn (Abbot) on Feb 20, 2011 at 23:22 UTC
    open(my OUT,">$peptstr1") or die $!; open(my IN,$genpept);

    my OUT and my IN are syntax errors and won't work.    You need to use scalar variables for that to work.    Also, you should always verify that the file opened correctly:

    open my $OUT, '>', $peptstr1 or die "Cannot open '$peptstr1' because: +$!"; open my $IN, '<', $genpept or die "Cannot open '$genpept' because: $ +!";



    while (my $records = <IN>){ #$i++; chomp $records; my @genpept; push @genpept,($records); print"Processed infile record: $.\n"; foreach(@genpept){ print "$_"; } }

    You don't print to the OUT filehandle so everything is going to STDOUT and not your file.

Re: How can i print to my outfile handle?
by Corion (Patriarch) on Feb 20, 2011 at 21:30 UTC
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: How can i print to my outfile handle?
by broomduster (Priest) on Feb 20, 2011 at 23:31 UTC
    I keep getting the error that my filehandle is closed.
    If I may be so bold.... The errors you should be getting from your posted code look like this:
    No such class OUT at pm-889248 line 5, near "open(my OUT" syntax error at pm-889248 line 5, near "my OUT," No such class IN at pm-889248 line 6, near "open(my IN" syntax error at pm-889248 line 6, near "my IN,"
    Note that these are not errors about closed file handles. Now, I humbly suggest that you follow the advice given by Corion and umasuresh and (re?)read the documentation they point you to.
Re: How can i print to my outfile handle?
by GrandFather (Saint) on Feb 20, 2011 at 23:56 UTC

    For future reference:

    • Only post code that you have actually run and copy/paste the code into your node.
    • Copy/paste the actual error message you received when running the code into your node.
    • If you need data to show the issue, trim the data to the smallest amount that shows the error and copy/paste that into your node or, even better, include the data in your sample code (in a __DATA__ section perhaps).
    True laziness is hard work
Re: How can i print to my outfile handle?
by umasuresh (Hermit) on Feb 20, 2011 at 22:23 UTC
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: How can i print to my outfile handle?
by Monkomatic (Sexton) on Feb 21, 2011 at 00:48 UTC

    As the Previous reply put it so succinctly.

    "True laziness is hard work "

    I'm going to copy my post to the previous question in the hopes that it might help you. My guess is you are over thinking the problem.

    QUESTION: Hello everyone. I need some help with creating and writing to a new file.

    I have a text file in the following format: sku, itemname, itemdescription, price

    I want to read each line, then write the contents of the line to a new file that is named sku.html

    So for the following example source file: 123, soap, dove, $1.99 I want to save that line to a file named 123.html

    I have the basics of how to open and read a file, and how to write to a new file. I just can't figure out how to name the new files dynamically after the sku field. Any suggestions are greatly appreciated. Thanks!

    My post:

    It's

    $chunklookup = "Datafile.txt"; open(DGRAB, $chunklookup) || die ("Could not open $chunklookup (DATA T +O GRAB) \n"); while (<DGRAB>) {push(@datain, $_)};close DGRAB; foreach @datain {$currentline =$_; @linesplitfetch = split(/,/, $currentline); open (MYFILE6, '>@linesplitfetch[0].html')|| die ("Could not open @lin +esplitfetch[0].html \n"); print MYFILE6 "Durka Durka Durka $currentline here ";close(MYFILE6); } # foreach @datain

    Go Team America!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-03-28 20:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found