$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
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Count similar characters in a row
by toolic (Bishop) on Oct 06, 2010 at 15:23 UTC | |
Re: Count similar characters in a row
by johngg (Canon) on Oct 06, 2010 at 17:44 UTC | |
by $new_guy (Acolyte) on Oct 07, 2010 at 11:33 UTC | |
by johngg (Canon) on Oct 07, 2010 at 13:50 UTC | |
by $new_guy (Acolyte) on Oct 08, 2010 at 08:19 UTC | |
by johngg (Canon) on Oct 08, 2010 at 09:15 UTC | |
| |
Re: Count similar characters in a row
by suhailck (Friar) on Oct 06, 2010 at 15:26 UTC | |
by jwkrahn (Abbot) on Oct 06, 2010 at 16:00 UTC | |
Re: Count similar characters in a row
by jwkrahn (Abbot) on Oct 06, 2010 at 15:30 UTC |
Back to
Seekers of Perl Wisdom