博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Perl语言之统计特定字符串个数
阅读量:7193 次
发布时间:2019-06-29

本文共 2870 字,大约阅读时间需要 9 分钟。

hot3.png

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 =  if (defined $opt_cn);    $opt_rn = 0 if (!defined $opt_rn);    $j = 0;    while () {        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 =  if (defined $opt_cn);    $j = 0;    $opt_rn = 0 if (!defined $opt_rn);    while () {        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

你可能感兴趣的文章
学习进度条
查看>>
uwsgi使用ini配置文件启动报错 no app loaded
查看>>
Linux命令之dig命令挖出DNS的秘密
查看>>
WordCount
查看>>
Qt游戏编程_07
查看>>
Java Socket实战之五:使用加密协议传输对象
查看>>
oracle删除表数据的两种的方式
查看>>
40.Node.js Web 模块
查看>>
centos7 ping: www.baidu.com: Name or service not known
查看>>
直接双击启动tomcat中的startup.bat闪退原因及解决方法
查看>>
45. ExtJS ComboBox 下拉列表详细用法
查看>>
串口开发
查看>>
拓补排序之烦人的幻灯片(确实很烦人。。。)
查看>>
关于HTTPService跨域安全策略的问题的解决
查看>>
[转]定制Android-Lint检查问题的现有规则
查看>>
递归实现全排列
查看>>
linux异步拷贝
查看>>
Trie(前缀树)和ternary trie和binary search tree
查看>>
thinkphp漏洞集合
查看>>
JAVA 并发
查看>>