MadraghRua has asked for the wisdom of the Perl Monks concerning the following question:
As the file is read, I would like to count the number of occurances of the word 'Algorithm' at the beginning of a line. If it occurs, I want to increment a counter.
If the words 'Experiment Name' occurs at the beginning of a line, the if loop exits and the subroutine returns the value of the counter.
My problem is that it only counts the first occurance of 'Algorithm' - the rest are ignored. Does anyone have a suggestion on what to change?sub getExpNumber { my ($filePath) = @_; my ($n) = 0; open (INPUT, $filePath) or die "Can't open $filePath: $!\n"; while (<INPUT>) { chomp; if (<INPUT> =~ /^Experiment Name/) { print "There are $n experiments in $filePath: $!\n"; } elsif (<INPUT> =~ /^Algorithm/) { $n++; next; } else { next; } close(INPUT) or die "Can't close $filePath.$!\n"; } return $n; }
I am running with use strict and I am using the -w on the #! line
Thanks
MadraghRua
yet another biologist hacking perl....
|
|---|