open my $fh, '<', 'C:\ppp.txt', or die "Could not open file $!"; while (my $line = <$fh>) { foreach my $str ($line) { if (index($line, $address) != -1) { $counter++; $flag='1'; } } } #### open my $fh, '<', 'C:\ppp.txt', or die "Could not open file $!"; if ($fh contains an empty file) { // then add this the $address in the ppp.txt's first line }