You would need to declare $count with the my operator within the getISCount subroutine but before entering the loops.

Also reading again all your files to get the total count can be very inefficient if your files are large, whereas you only need to accumulate the individual line counts. Perhaps something like this, which stores the file names and file line counts in a hash for further use:

use strict; use warnings; use Data::Dumper; my $total_count; my %files = map {$_, 0} glob "./s*.pl"; for my $key (keys %files) { my $count = getcount($key); $files{$key} = $count; $total_count += $count; } sub getcount { local @ARGV = @_; while (<>) {}; return $.; } print Dumper \%files; print "Total size = $total_count lines.\n";
which prints:
$ perl line_count.pl $VAR1 = { './sum.pl' => '19', './summary.pl' => '16', './strict.pl' => '15', './schwartz2.pl' => '13', './sort_hash.pl' => '12', './separat.pl' => '28', './subdiscard3.pl' => '12', './schwartz.pl' => '27', './subdiscard2.pl' => '54', './scope.pl' => '16', './serv.pl' => '28', './shuffle.pl' => '21', './subdiscard.pl' => '23' }; total size = 284 lines.
If you only need to print the counts and don't need to store the values for further use, this can be simplified as follows:
use strict; use warnings; my $total_count; my @files = glob "./s*.pl"; for my $file (@files) { my $count = getcount($file); print "$file \t $count \n"; $total_count += $count; } sub getcount { local @ARGV = @_; while (<>) {}; return $.; } print "Total size = $total_count lines.\n";
which prints this:
$ perl line_count.pl ./schwartz.pl 27 ./schwartz2.pl 13 ./scope.pl 16 ./separat.pl 28 ./serv.pl 28 ./shuffle.pl 21 ./summary.pl 16 ./sort_hash.pl 12 ./strict.pl 15 ./subdiscard.pl 23 ./subdiscard2.pl 54 ./subdiscard3.pl 12 ./sum.pl 19 Total size = 284 lines.


In reply to Re: While loop double counting by Laurent_R
in thread While loop double counting by vihar

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.