Hello monks,
I have some texts that looks like this:
<td bgcolor="white"></td> <td bgcolor="white"></td> <td bgcolor="white"></td> <td bgcolor="white"></td> <td bgcolor="white"></td> <td bgcolor="green" height="10"></td> <td bgcolor="green" height="10"></td> <td bgcolor="green" height="10"></td> <td bgcolor="green" height="10"></td> <td bgcolor="green" height="10"></td> <td bgcolor="white"></td> <td bgcolor="white"></td> <td bgcolor="white"></td> <td bgcolor="white"></td> <td bgcolor="green" height="10"></td> <td bgcolor="green" height="10"></td> <td bgcolor="white"></td> <td bgcolor="white"></td> <td bgcolor="white"></td>
I need to count the number of "green" lines. In the above example, my output should look something like:
Previously I've written a code that counts the occurrence of "green" if it occurs only once. My code is a bit silly, and I can't really explain it without being wordy, so I'll just put it here.First green = line 6 to 10 Second green = line 15 to 17
#!/usr/bin/perl use warnings; open (GREEN,"data.txt"); open (WHITE,"data.txt"); $/ = "</td>"; $counter_white = 0; $counter_green = 0; #----------------count number of green--------------------- while ($line_green = <GREEN>) { if ($line_green =~ /<td bgcolor="green" height="10"><\/td>/){$coun +ter_green++;} } #------count number of white (before any green occur)------------- while ($line_white = <WHITE>) { if ($line_white =~ /<td bgcolor="white"><\/td>/){$counter_white++; +} if ($line_white =~ /<td bgcolor="green" height="10"><\/td>/){last; +} # escape when green starts } #--------------------------------------- print "white = ".$counter_white."\n"; print "green = ".$counter_green."\n"; $beginning = $counter_white + 1; $end = $counter_white + $counter_green; print $beginning."\n"; # start of green print $end."\n"; # end of green print "The result starts from ".$beginning." to ".$end."\n"; close (GREEN); close (WHITE);
In reply to counting string in a text file by nurulnad
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |