Perl语言之统计特定字符串个数

amount.pl:

#!/usr/bin/perl
################################################################################
#                  use for calculate the amount of w or not
#
#
#
################################################################################
use warnings;
use strict;
use Getopt::Long;

sub usage{
    print STDOUT "use for calculate the amount of w or not\n";
    print STDOUT "perl $0 -a prof -w character -n -c -r -cn -rn -h\n";
    print STDOUT "\t-a str file to calculate, required\n";
    print STDOUT "\t-w str character to search, required\n";
    print STDOUT "\t-n calculate amount of not w\n";
    print STDOUT "\t-c calculate by column\n";
    print STDOUT "\t-r calculate by row\n";
    print STDOUT "\t-cn the file contain colnames\n";
    print STDOUT "\t-rn the file contain rownames\n";
    print STDOUT "\t-h help info\n";
    exit(1);
}

our ($opt_a, $opt_w, $opt_n, $opt_c, $opt_r, $opt_cn, $opt_rn, $opt_h);
my (@total_c, @total_r, @seq);
my ($i, $j, $cn);

GetOptions("a:s" => \$opt_a,  # 指定文本
           "w:s" => \$opt_w,  # 指定字符串
           "n" => \$opt_n,  # 统计不是指定字符串的个数
           "c" => \$opt_c,  # 按列统计
           "r" => \$opt_r,  # 按行统计
           "cn" => \$opt_cn,  # 指定文本有列名
           "rn" => \$opt_rn,  # 指定文本有行名
           "h" => \$opt_h);  # 帮助

&usage unless(defined $opt_a && defined $opt_w);
&usage unless($opt_c || $opt_r);
&usage if($opt_h);

if (defined $opt_c) {
    open I, $opt_a or die "can not open $opt_a";
    $cn = <I> if (defined $opt_cn);
    $opt_rn = 0 if (!defined $opt_rn);
    $j = 0;
    while (<I>) {
        chomp;
        @seq = split/\t/;
        $j = $#seq if ($#seq - $opt_rn > $j);
        for ($i = $opt_rn; $i <= $#seq; ++$i) {
            if (defined $opt_n) {
                ++$total_c[$i - $opt_rn] if ($seq[$i] ne $opt_w);
            } else {
                ++$total_c[$i - $opt_rn] if ($seq[$i] eq $opt_w);
            }
        }
    }
    if (defined $cn) {
        print $cn;
        @seq = split /\t/, $cn;
        print "\t" if ($seq[0] eq "");
    }
    $total_c[0] = 0 if (!defined $total_c[0]);
    print "$total_c[0]";
    for ($i = 1; $i <= $j - $opt_rn; ++$i) {
        $total_c[$i] = 0 if (!defined $total_c[$i]);
        print "\t$total_c[$i]";
    }
    print "\n";
    close I;
}

if (defined $opt_r) {
    open I, $opt_a or die "can not open $opt_a";
    $cn = <I> if (defined $opt_cn);
    $j = 0;
    $opt_rn = 0 if (!defined $opt_rn);
    while (<I>) {
        chomp;
        @seq = split/\t/;
        $total_r[$j] = 0;
        for ($i = $opt_rn; $i <= $#seq; ++$i) {
            if (defined $opt_n) {
                ++$total_r[$j] if ($seq[$i] ne $opt_w);
            } else {
                ++$total_r[$j] if ($seq[$i] eq $opt_w);
            }
        }
        if ($opt_rn) {
            print "$seq[0]\t$total_r[$j]\n";
        } else {
            print "$total_r[$j]\n";
        }
        ++$j;
    }
    close I;
}



转载于:https://my.oschina.net/u/1791586/blog/289661

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值