nurulnad has asked for the wisdom of the Perl Monks concerning the following question:
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);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: counting string in a text file
by ELISHEVA (Prior) on Jan 13, 2011 at 04:58 UTC | |
|
Re: counting string in a text file
by jwkrahn (Abbot) on Jan 13, 2011 at 07:30 UTC | |
|
Re: counting string in a text file
by eff_i_g (Curate) on Jan 13, 2011 at 05:09 UTC | |
|
Re: counting string in a text file
by PeterPeiGuo (Hermit) on Jan 13, 2011 at 04:49 UTC | |
|
Re: counting string in a text file
by locked_user sundialsvc4 (Abbot) on Jan 13, 2011 at 13:10 UTC |