in reply to Counting occurances of a pattern in a file

You are reading a new line each time  <INPUT> appears. Change it to something like:
while(<INPUT>) { $n+=/^Algorithm/; last if /^Experiment/ }