字符串分割,计算,倒序排列

1200阅读 0评论2014-01-05 yestreenstars
分类:LINUX

原帖:我转载到Shell版的帖:

处理前:
ABDCDCDC

处理后:
DC      3       14
AB      1       1

处理规则:
请看原帖。


  1. awk -F '' -vK=2 '{for(i=1;i<=NF;i+=K){y=NF-i


点击(此处)折叠或打开

  1. #!/usr/bin/perl
  2. use warnings;
  3. use strict;
  4. use 5.010;

  5. my %hash = ( A=>0, B=>1, C=>2, D=>3 );
  6. my $str = "ABDCDCDC";
  7. my $K = 2;
  8. my %times;
  9. my $i = 0;
  10. ++ $times{ substr $str, $i, $K } and $i += $K while $i < length $str;
  11. map { say join "\t", @$_ } sort { $b->[1] <=> $a->[1] } map {
  12.         my @each = split //, $_;
  13.         my $sum = 0;
  14.         map { $sum += $hash{ $each[$_] } * 4 ** ( $#each - $_ ) } 0 .. $#each;
  15.         [ $_, $times{$_}, $sum ];
  16. } keys %times;


上一篇:按组寻找数值的问题
下一篇:Error:No suitable device found: no device found for connection "System eth1"