in reply to Counting occurances of a pattern in a file
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; }
|
|---|