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:

First green = line 6 to 10 Second green = line 15 to 17
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.
#!/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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.