in reply to counting string in a text file
This is homework? (you would never have <td> tags in isolation like this in a real HTML file since they are part of a larger table definition). A few hints to get you back on track:
You don't need two separate filehandles to print the start and length of the first green run and the first white run. In fact it is impossible to do with two separate streams, one only counting white and one only counting green, because you need to know when the green stops and the white begins and vice versa. That requires a single sequence of lines so you know when you've changed from green to white and back again.
It is all in the variables. Try thinking about how you could find the start of a color run and count its length with one file handle and the following variables: $startOfRunLineNumber, $currentColor, and $currentLineNumber.
What changes to tell you that it is the end of the run? The color? Of course. So inside your loop you need an if...elsif...else or if...else that checks the color of the current line against the stored value of $currentColor set while reading the previous line.
Please get in the habit of using the three parameter open:open MYDATA, "<", "data.txt", where the open mode (parameter 2) is separate from the file name (parameter ). It pays to be explicit about what you want Perl to do with the file. In the two parameter open, Perl has to make guesses about whether the string contains a file name, an open mode, or both. If the guess is wrong , you could get burned.
If this is not an exercise and you are parsing live HTML, then please, please, please learn how to use an HTML parser package like HTML::Parser. Normal HTML files can't be reliably parsed using regular expressions due to its repetitive nested nature.
|
|---|