http://www.perlmonks.org?node_id=863806

$new_guy has asked for the wisdom of the Perl Monks concerning the following question:

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