my $tomD_phil; etc... foreach (@line_of_input_file) { if (($_ =~ /tomD/) and ($_ =~ /phil/)) { $tomD_phil++ } etc... }