Dear perl users,
I have a file from which I would like to create n sub-files and extract data from these sub-files. I am able to generate the n sub-files. But, I can't extract data from one of the sub-files (the last one). However, when I write the section of the code involved in extracting data from the sub-files into a separate script, I can get the data I am looking for. Can someone please let me know why this is happening?
Here's part of the script:
my @file = <files*.txt>;
my $file;
my ($sum, $cti, $avg);
my $tmp = "tmp.txt";
foreach $file (@file)
{
$sum = 0; $cti = 0; $avg = 0;
open(FIL,"$file");
open(TMP,">$tmp");
while(<FIL>)
{
chomp;
my ($hd, $md, $tl) = split(/\|/,$_);
$sum += $tl;
$cti++
}
$avg = $sum / $cti;
open(FIL,"$file");
while(<FIL>)
{
chomp;
my ($hd, $md, $tl) = split(/\|/,$_);
$tl = $tl / $avg;
my $tlf = sprintf("%.4f",$tl);
print TMP "$hd\|$md\|$tlf\n";
}
rename("$tmp","$file");
}
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.