The following fragment is extracted from your program:
sub sep_id {
...
open (TMPFILE, "> $tmp_file") or (die "Could not open $tmp_fi
+le: $!");
select TMPFILE;
foreach $water (keys %wat_freq) {
$freq=0;
$code=undef;
if ($wat_freq{$water} >= $C_NETSIZE) {
@ids=();
foreach (@info) {
if (substr($_,13,5,) == $water ) {
push @ids, substr($_,8,5);
}
}
@ids=sort {$a <=> $b} @ids;
$code = join ":", $water . "+", @ids;
$code =~ s/ /0/g;
printf "%02d:+%s:\n",$wat_freq{$water}, $code;
}
}
select STDOUT;
You are missing a
close TMPFILE after you created the file. Now what happens is that the last file created will not closed properly, although files created earlier are automatically closed by the next open of the TMPFILE in your loop.
Update:
Having looked at your code, which is ugly, I just want to add a few comments:
1) Add
use strict; to the beginning of your code to prevent autovivification of variables.
2) Avoid using global variables. And if you must, try to make your intension clear on how the global variables are used. For example, instead of doing
my $SOME_GLOBAL_VARIABLE = 0;
some_sub_with_side_effects();
...
sub some_sub_with_side_effects {
$SOME_GLOBAL_VARIABLE = 1;
}
Do this instead:
my $SOME_GLOBAL_VARIABLE = some_sub_without_side_effects();
...
sub some_sub_without_side_effects {
return 1;
}
3) Remove the { } around the function calls, they are unnecessary and ugly.
4) Use
IO::File my $f = new IO::File "file.txt", "w" or
open my $f, "< file.txt" when openning your files. In other words, avoid using direct file handles, use lexical/anonymous file handles that are associated to the variables they are attached to. The lexical file handles are closed automatically when the associated variables fall out of scope, so you don't have to worry about closing them.
5) When modifying a Perl built-in variable, always create a scope and localize it first. You had a
$| = 1; in your sub that turned off bufferring, however the effect is not cancelled when your sub finishes, this side effect carries on to other subs. This will not happen if you do
local $| = 1;
Anyway, good luck and happy Perl programming.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.