Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re: Opening and closing files in loop

by BrowserUk (Pope)
on Oct 22, 2012 at 08:30 UTC ( #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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1000289]
[1nickt]: ( The 17" monster I just bought is for my wife, who doesn;t leave the house with it...)
[Corion]: 1nickt: No, I'm using it with Windows 10, but with the 4k display, yes. It's a very nice build IMO, and relatively sturdy. Also, it got 8 or 9 on the ifixit repair scale, which I consider good
[Corion]: ... especially as I expect to have to replace the battery set in two or three years' time
[1nickt]: Nice. So happy that Dells are still user-repairable. Unusual these days.
[1nickt]: I will certainly not be buying any more Macs.
[Corion]: 1nickt: Yeah, or at least, I hopoe so ;)

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (13)
As of 2017-03-27 13:13 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (320 votes). Check out past polls.