Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: What is wrong in this code???

by CountZero (Bishop)
on Dec 08, 2012 at 11:28 UTC ( #1007889=note: print w/ replies, xml ) Need Help??


in reply to What is wrong in this code???

What about this?

use Modern::Perl; use Data::Dump qw /dump/; my @results; while (<DATA>) { my @codon = split '', (split /\s+/)[1]; while (my ($seq, $char) = each @codon) { $results[$seq]{$char}++; } } say dump(@results); __DATA__ first ACGATT--GAT second AGTTAAC-TTT third TTAGCAGG-TA
Output:
( { A => 2, T => 1 }, { C => 1, G => 1, T => 1 }, { A => 1, G => 1, T => 1 }, { A => 1, G => 1, T => 1 }, { A => 1, C => 1, T => 1 }, { A => 2, T => 1 }, { "-" => 1, "C" => 1, "G" => 1 }, { "-" => 2, "G" => 1 }, { "-" => 1, "G" => 1, "T" => 1 }, { A => 1, T => 2 }, { A => 1, T => 2 }, )
Alternatively you can write the while loop more compact (but less easy to read)
while (<DATA>) { my $seq; $results[$seq++]{$_}++ for split '', (split /\s+/)[1]; }

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

My blog: Imperial Deltronics


Comment on Re: What is wrong in this code???
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (16)
As of 2014-09-16 14:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (24 votes), past polls