Hi,
I am trying to do a simple thing here. I have 7 files that I am reading from directory. I need to be able to get number of lines for each file and then another number that has a total of lines counts for all 7 files.
Here is my code
#Getting all individual files and number of total files
@ISFiles = glob 'dir1/file.split.*';
$ISNumFiles = scalar(@ISFiles);
#getISCount() function that gets count for each file
sub getISCount {
my @files = @_;
foreach $file (@files) {
open(FILE, $file) or die "Can't open `$file': $!";
while (sysread FILE, $buffer, 4096) {
$count += ($buffer =~ tr/\n//);
}
close FILE;
}
return $count;
}
$i = 0;
$j = 1;
while($i < scalar(@ISFiles)) {
$ISCountForFile[$j] = &getISCount(@ISFiles[$i]);
$i = $i + 1;
$j = $j + 1;
}
$combinedCount = &getISCount(@ISFiles);
print "combined count - $combinedCount\n";
The problem is when these individual counts are returned, the count for the second file is always count for the first file + count for second file and so on... I am not sure why it is incrementing.
Also, the final $combinedCount should just be a sum of all individual files. Instead, it returns the sum of (lines file1 + (lines file1 + lines file2) + ... )
For example,
If following are my line counts:
File1 = 10
File2 = 10
File3 = 10
The result I should be getting is
FileCount1 = 10
FileCount2 = 10
FieCount3 = 10
$combinedCount = 30
Instead, I am getting this:
FileCount1 = 10
FileCount2 = 20
FileCount3 = 30
$combinedCount = 60
Any idea why it is double counting?
Thanks
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.