Sunday, June 8, 2014

How do you count number of lines in a file?

my $msg;

open(IN, "<", "$0") or die "Can't open $0 - $!";
while (<IN>) {
$msg = $msg . $_;
}
close IN;

$count = () = $msg =~ /\n/g;
print "Line Count - $count\n";

#or

$lines = () = $msg =~ m/\n/g;
print "Line Count - $lines\n";

No comments :