in reply to Counting occurances of a pattern in a file

Your flow is off. Make finding "Experiment Name" the exception rather than the norm; don't use next to say when you want to repeat the loop, use last to break out of it. And put the close outside of the loop, as well.

Your other problem is that you're reading the line multiple times in the same loop, then discarding some of the results. Each time you read from the file using the diamond ops (<>), you're reading a line. A *new* line.

Try this:

sub getExpNumber { my $filePath = shift; my $n = 0;   open (INPUT, $filePath) or die "Can't open $filePath: $!\n"; while (<DATA>) { if (/^Experiment Name/) { last; } elsif (/^Algorithm/) { $n++; } } close(INPUT) or die "Can't close $filePath.$!\n"; print "There are $n experiments in $filePath\n"; return $n;  }