用perl来统计字符串中字母个数的代码,感兴趣的朋友可以参考下。
#!/usr/bin/perl -w
use strict;
my $str = "abcsfsaf#sdagasdga#sdgaghoiiopjh#dsfjkopa hkl;fjh#dsjfklpafj";
my $count = 0;
while( $str =~ /#/g )
{
$count++;
}
print "num of # is: $countn";
要统计其他字符,把pattern替换即可了
grep -c
代码:
代码:
代码:
!/usr/bin/perl -w
# Purpose: to calculate the frequence of any words in a file
use strict;
my %fre;
while (<>) {
chomp;
foreach ( split ) {
$fre{$_} += 1;
}
}
foreach (keys %fre) {
print "$fre{$_}t$_n";
}