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


in reply to How can I count the number and kinds of letters at 1st, 2nd and 3rd positions of 3-letter words in a string?

First, read davido's answer: Re: How can I count the number and kinds of letters at 1st, 2nd and 3rd positions of 3-letter words in a string?

Here, I put some code, as easy as possible (I hope) without complexe data structure:

#!perl use strict; use warnings; my $seq = "ATCGGCGCCTAT" ; my (%first,%second,%third); #perlre my @trilet = $seq =~ /.../g; #perlsyn LOOP foreach my $letter ('A','T','G','C') { #init $first{ $letter }=0; $second{ $letter }=0; $third{ $letter }=0; } foreach my $tri (@trilet) { #perlfunc : substr $first{ substr $tri,0,1 }++; $second{ substr $tri,1,1 }++; $third{ substr $tri,2,1 }++; } foreach my $letter ('A','T','G','C') { print "$letter=$first{$letter}; "; } print "\n"; foreach my $letter ('A','T','G','C') { print "$letter=$second{$letter}; "; } print "\n"; foreach my $letter ('A','T','G','C') { print "$letter=$third{$letter}; "; } print "\n";