如何通过perl脚本计算每行中某些单词的出现次数?

时间:2017-04-19 10:42:01

标签: perl

该文件包含多行,如下所示

  TT      AA      TT      GG      CC      AA
  AA      GG      CC      TT      GG      CC
  GG      AA      TT      TT      CC      TT

1 个答案:

答案 0 :(得分:1)

这样的事可能吗?

#!/usr/bin/env perl;
use strict;
use warnings;

while ( <DATA> ) { 
    chomp;
    my %count_of;
    $count_of{$_}++ for split; 

    print "Row $.:\n";
    foreach my $key ( sort {$count_of{$b} <=> $count_of{$a} } keys %count_of ) { 
       print "\t$key: $count_of{$key}\n";
    }
}

__DATA__
  TT      AA      TT      GG      CC      AA
  AA      GG      CC      TT      GG      CC
  GG      AA      TT      TT      CC      TT