Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Re: Opening and closing files in loop

by Corion (Pope)
on Oct 22, 2012 at 08:33 UTC ( #1000290=note: print w/replies, xml ) Need Help??

in reply to Opening and closing files in loop

You don't show the value(s) of $i and $max, so maybe there is a simple reason why your loop only gets run once.

Personally, I would change the coding style in two places to avoid nasty errors:

First, I would not use the C-style for construct but iterate over a range. I'd also use a lexical $i to avoid clobbering the loop counter through side-effects:

for my $i (1..$max) { ... };

Second, I would avoid using select for choosing the active output handle and print to the filehandles directly:

print DB qq("$dept$w$i, ..."\n); print newDB qq("$dept$w$i, ..."\n);

While rewriting your code to the new idiom, I saw that you seem to be running without warnings. As soon as you add use warnings; to the top of your script, or add -w to the Perl invocation, Perl will tell you what is going wrong.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1000290]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (9)
As of 2018-06-23 16:24 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (125 votes). Check out past polls.