Dear Monks,
I would like to count all the r's in the rows (row by row) then print to screen only the number of rows that have r's on all columns (i.e. 3 in this case). How do I do this. My script below doesn't seem to work well.
Thanks
script:
use strict;
use warnings;
# declare variables
my @column = ();
my $y;
my %hash;
my $count;
my $range = 3;
# open the file
open(INFILE,"<","my_column_file.txt") or die("Couldn't open the file\n
+");
while (<INFILE>) {
print "\n"; #separate the elements in each row
+with a newline
for ($y=0; $y < 3; $y++){ # from the first to the last field
+ (ie field/column 3)
my ($field) = (split)[$y]; # extract fields/ columns from the
+ data
push @column, $field; # store all the field values in an
+ a array
#count the consecutive r's --> all those common to a row
#(i.e.if a sinlge specified column misses an r at that row do
+n't cont it)!
while ($field =~ /(r+)/g) {
my $new_count = $count++;
print "$new_count\n";
}
print pop@column; # print from the array, each time r
+emoving the printed element (avoiding repetition)
print "\n"; # print each element in a new line
}
}
my file:
0 a b h
1 - r z
3 u - u
4 r x r
5 r t r
6 r r r
7 r r r
8 r r r
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|