Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: creating a text file at run time and writing to it.

by phenom (Chaplain)
on Jul 20, 2006 at 11:22 UTC ( #562546=note: print w/ replies, xml ) Need Help??


in reply to creating a text file at run time and writing to it.

I'd probably do this:

#!/usr/bin/perl use strict; use warnings; open( NAMES, "<", "name.txt") or die $!; my @list = <NAMES>; close(NAMES); print "list = @list\n"; for my $name (@list) { open(OUT, ">", $name) or die $!; print OUT "hello"; close(OUT); }
Notice the differences:
1. use strict and use warnings; 2. use the "$!" so you get the explicit error when there is one 3. you didn't define $y - and it looks to be unnecessary for this 4. you didn't close the FILE handles 5. you need to specify the FILE handle you want to write to
Start with "use strict" and "use warnings" and that will be very helpful!
hth, dave

Update: Oops, forgot the .txt part - see madtoperl's node.


Comment on Re: creating a text file at run time and writing to it.
Select or Download Code
Re^2: creating a text file at run time and writing to it.
by Anonymous Monk on Jul 20, 2006 at 11:33 UTC
    Hi Thank you for the response but gives mesg " invalid argument" at line 11
      Ananymous Monk,

      You have to do some changes in the code,as per that it wont create file with .txt.so use this code,it will work,
      use strict; use warnings; open( NAMES, "<name.txt") or die $!; my @list = <NAMES>; close(NAMES); print "list = @list\n"; for my $name (@list) { chomp($name); open(OUT, ">$name.txt") or die $!; print OUT "hello"; close(OUT); }
      Thanks and Regards,
      madtoperl.
        Hi i was trying to do some calculation while the files created, and it prints to screen too. but when i tried to print it to the files have created its not happening.
        Hi i am sorry i got that sorted out.it was someother looping problem Thank you

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2014-07-10 06:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (200 votes), past polls