my @matches; while () { push @matches, $_ if /formulae/; last if scalar @matches == 2; }