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


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