Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Opening and closing files in loop

by BrowserUk (Patriarch)
on Oct 22, 2012 at 08:30 UTC ( [id://1000289]=note: print w/replies, xml ) Need Help??


in reply to Opening and closing files in loop

Is there a restriction in opening and closing files inside a loop?

You aren't opening the files within the loop.

select only makes a particular, already open, filehandle the default for print.

But you are closeing those handles on the first pass, and they never get re-opened.

Two possibilities:

  1. Don't close the files, just select between then:
    for ($i=1;$i<=$max;$i++){ $pwd = &randomPwd($nmbchars); select (DB); print qq("$dept$w$i","7 days","$pwd",,,,,,\n); select (newDB); print qq("$dept$w$i"\t>\t"$pwd"\n); }
  2. Instead of selecting between them, supply the required file handle to the print statements:
    for ($i=1;$i<=$max;$i++){ $pwd = &randomPwd($nmbchars); print DB qq("$dept$w$i","7 days","$pwd",,,,,,\n); print newDB qq("$dept$w$i"\t>\t"$pwd"\n); }

Personally, I prefer the latter option.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

RIP Neil Armstrong

Replies are listed 'Best First'.
Re^2: Opening and closing files in loop
by lt007 (Initiate) on Oct 22, 2012 at 08:42 UTC
    Thank you very much!!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-25 16:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found