境随心转
chinaorg
全部博文(3)
perl(2)
system(1)
2008年(3)
bjchenxu
厚积薄发
分类:
2008-11-25 01:12:25
#!/usr/bin/perlprint ("\t统计字符出现次数\n请输入英文字符:回车ctl+d 结束输入:\n");while ($line = <STDIN>){ $line =~ tr/A-Z/a-z/; $line =~ s/[^a-z]//g; @letters=split(//,$line); foreach $letter (@letters){ $lettercount{$letter}+=1; }}$i=0;sub occurrences{ #print ("调用第[".++$i."]次:$a=>$lettercount{$a} $b=>$lettercount{$b}\n"); $lettercount{$a}<=>$lettercount{$b};}print ("\n原始哈希表元素列表 :\n");while (($key, $var) = each(%lettercount)) { print ("$key=>$var\n");}print ("\n哈希表按字符排序:\n");foreach $word (sort keys(%lettercount)) { print ("$word=>$lettercount{$word}\n"); }print("\n按字符出现次数排序\n");foreach $word (reverse sort occurrences(sort keys(%lettercount))) { print ("$word=>$lettercount{$word}\n"); }
上一篇:perl 二叉树示例程序
下一篇:没有了
登录 注册