Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Unable to write to file

by vasavi (Initiate)
on May 10, 2011 at 14:15 UTC ( [id://903982]=perlquestion: print w/replies, xml ) Need Help??

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

Hi I am trying to run the below code. I am able to get the output on to the console but unable to print it to the a outputfile. To me everything seems fine. Please help me with this code. I have given example of input_at.txt and input_ac.txt
#!usr/bin/perl use strict; use warnings; use diagnostics; open(FHAT, "input_at.txt") || die("Could not open file!"); open(OUTFH, ">outputfile.log") || die("Could not open file!"); while(<FHAT>){ chomp($_); print "$_\n"; print OUTFH "$_\n"; my ($at,$id1)=split(/\,/, $_); open(FHAC, "input_ac.txt") || die("Could not open file!"); while (<FHAC>){ print "ac $_"; my ($id2,$ac)=split(/\,/, $_); if($id2 == $id1){ print "SUCCESS $at : $ac : $id1"; print OUTFH "SUCCESS $at : $ac : $id1"; } } close (FHAC); } close (FHAT); close (OUTFH);
input_at.txt aaa,1 bbb,2 ccc,3 input_ac.txt 1,a 3,b 2,c

Replies are listed 'Best First'.
Re: Unable to write to file
by toolic (Bishop) on May 10, 2011 at 14:26 UTC
    but unable to print it to the a outputfile
    It works for me at my unix prompt. It creates "outputfile.log" with these contents:
    aaa,1 bbb,2 ccc,3 SUCCESS aaa : a 3 : 1 bbb
    It also prints this output with warnings:
    aaa,1 bbb,2 ccc,3 ac 1,a 3,b 2,c Argument "1 bbb" isn't numeric in numeric eq (==) at z line 20, <FHAC> + line 1 (#1) (W numeric) The indicated string was fed as an argument to an oper +ator that expected a numeric value instead. If you're fortunate the me +ssage will identify which operator was so unfortunate. readline() on closed filehandle FHAC at z line 20 (#2) (W closed) The filehandle you're reading from got itself closed so +metime before now. Check your control flow. SUCCESS aaa : a 3 : 1 bbb

    Please edit your post using code tags around your code and data. See Writeup Formatting Tips. Also post the output and any warning messages you get.

    Update: simplify your code to see if you can write to a file, and use autodie:

    use strict; use warnings; use diagnostics; use autodie; open( OUTFH, ">outputfile.log" ); print OUTFH "here is some output\n"; print OUTFH "here is more output\n"; close(OUTFH);
Re: Unable to write to file
by fidesachates (Monk) on May 10, 2011 at 15:32 UTC
    Based on your shebang line, I gather you're on a nix machine. issue a  touch file.txt to eliminate permission problems. Then issue a  ls > file.txt to eliminate space issues. After that, it's likely a perl problem.
Re: Unable to write to file
by papidave (Pilgrim) on May 10, 2011 at 22:52 UTC

    I'm assuming, based on the errors with == comparison of id1 and id2, that you meant to put newlines in your input files, e.g. for input_at.txt:

    aaa,1 bbb,2 ccc,3
    and for input_ac.txt:
    1,a 3,b 2,c

    Like the prior poster, I am also able to get this code to run on my Linux box, though the output is kind of munged. You may want to chomp the line read from FHAC before you split it, then put newlines explicitly where you want them in the output stream. I have seen funky errors with i/o when you don't insert newlines, though the close() call should flush all your streams for you.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2024-04-19 20:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found