Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

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

by johngg (Abbot)
on Apr 25, 2012 at 15:32 UTC ( #967092=note: print w/ replies, xml ) Need Help??


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?

Use a hash of hashes rather than individual scalar count variables. You could use unpack to break the string into words and a global regex match and capture for the individual positions.

knoppix@Microknoppix:~$ perl -Mstrict -MData::Dumper -wE ' > my $str = q{ATCGGCGCCTAT}; > my @words = unpack q{(a3)*}, $str; > my %counts; > > foreach my $word ( @words ) > { > my $posn; > $counts{ q{position } . ++ $posn }->{ $1 } ++ > while $word =~ m{(.)}g; > } > > print Data::Dumper->Dumpxs( [ \ %counts ], [ qw{ *counts } ] );' %counts = ( 'position 1' => { 'A' => 1, 'T' => 1, 'G' => 2 }, 'position 3' => { 'T' => 1, 'C' => 3 }, 'position 2' => { 'A' => 1, 'T' => 1, 'C' => 1, 'G' => 1 } ); knoppix@Microknoppix:~$

I hope this is helpful.

Cheers,

JohnGG


Comment on Re: How can I count the number and kinds of letters at 1st, 2nd and 3rd positions of 3-letter words in a string?
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://967092]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (10)
As of 2014-08-29 22:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (289 votes), past polls