Jeffrey Palm wrote:
>
> Digital Puer wrote:
> >
> > 1. How do you actually count this? On a Unix box, I can run 'wc -l'
> > to get the lines, but of course this includes comments and blank lines.
>
> I use this script:
Oooh! Perl code.
> my $total = 0;
> my $FORMAT = "%-40s%10d\n";
> foreach $infile (@ARGV) {
> open(infile, "<$infile") || die "Cannot open $infile:" . $!;
> my $cnt = 0;
> loop: while (<infile>) {
Perl has a special idiom 'while (<>)' that automatically opens and reads
from every file in @ARGV.
> chomp;
> s/^\s+//g;
> s/\s+$//g;
The /g option means to match "globally". In other words, to search the
entire string and find every match. However you are using the '^' and
'$' anchors which can only match at one place, the beginning and end of
the string respectively.
> foreach $a ("{","}","") {
> next loop if $_ eq $a;
> }
> my $loc = tr/;//;
> $cnt += $loc && !/^for/ ? $loc : 1;
> }
> printf $FORMAT, $infile, $cnt;
> $total += $cnt;
> close(infile);
> }
> print "-"x50 . "\n";
> printf $FORMAT, "TOTAL", $total;
This is more idiomatic Perl and is also faster in my tests.
my ( $cnt, $total );
my $FORMAT = "%-40s%10d\n";
while ( <> ) {
if ( eof ) {
printf $FORMAT, $ARGV, $cnt;
$total += $cnt;
$cnt = 0;
}
next unless /\S/;
next if /^\s*[}{]\s*$/;
my $loc = tr/;//;
$cnt += $loc && !/^\s*for/ ? $loc : 1;
}
print '-' x 50, "\n";
printf $FORMAT, 'TOTAL', $total;
__END__
John
--
use Perl;
program
fulfillment