Perl - line count of a file

Hi,

I am quite new to perl scripting.

I have a dat file (datFile) from which I am pulling only first column and saving the output to a new file (f). From that file (f) I am removing blank lines and saving it to new file (datFile1). I am able to get the count of $f file in variable $cnt. But I am unable to get the count of $datFile1 in $cnt1.

Please find below my code.

my $cnt;
my $cnt1;
my $f = $datFile."_Column.dat";
my $datFile1= $datFile."_null.dat";
open my $fh_in,  "<", "$datFile"  or die "Could not open $datFile: $!\n";
open my $fh_out, ">", "$f" or die "Could not create $f: $!\n";
while ( my $line = <$fh_in> ) {
    my $first_column = (split /\t/, $line)[0];
    print $fh_out "$first_column\n";
}
close $fh_in;
close $fh_out;
open my $fh_in1,  "<", "$f"  or die "Could not open $f: $!\n";
open my $fh_out1, ">", "$datFile1" or die "Could not create $datFile1: $!\n";
while ( <$fh_in1> ) {
    if (/\S/) {
        print $fh_out1 $_;
    }
}
open(FH,$datFile) or die "Damn. $!";
$cnt++ while <FH>;
close FH;
print "$cnt\n";
open(FHN,$datFile1) or die "Damn. $!";
$cnt1++ while <FHN>;
close FHN;
print "$cnt1\n";
$logger->info($ID." ".$cnt." Check count cnt");
$logger->info($ID." ".$cnt1." Check count cnt1");

Regards
Neethu

Is it possible that $datFile is holding/representing a file name without extension?

File Name of a sample $datfile - Sample_20141014_100.dat
File Name of sample $f - Sample_20141014_100.dat_Column.dat
File Name of sample $datFile1 - Sample_20141014_100.dat_null.dat

When I replace $datFile1 in the below code to $datFile then I am able to get the count for $datFile in $cnt1.

open(FHN,$datFile1) or die "Damn. $!";
$cnt1++ while <FHN>;
close FHN;
print "$cnt1\n";

Regards
Neethu

Please try

open(FHN,"$datFile1") or die "Damn. $!";

I changed to

open(FHN,"$datFile1") or die "Damn. $!";

but it didnt work.

---------- Post updated at 09:54 AM ---------- Previous update was at 08:58 AM ----------

Thank you for your time.

I am able to solve it.

Regards
Neethu