Output: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
Alternatively you can write the while loop more compact (but less easy to read)( { 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 }, )
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 DeltronicsIn reply to Re: What is wrong in this code???
by CountZero
in thread What is wrong in this code???
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |