my @matches; .... while(<>) { my @matches = $_ =~ m/$string/g; my $repetition = @matches; print "$string was encountered $repetition times.\n" if($repetition > 0); }