my %hash_one = { 'string_one' => 0, 'string_two' => 0, 'string_three' => 0, }; @ARGV = qw(file.txt); close ARGV; while (<>) { for $key (keys %hash_one) { $hash_one{ $key }++ if (/$key/); } }