如何在字符串数组上获得一些字母表&在perl中计算一些字母表

时间:2017-04-08 13:59:50

标签: perl

  

例如:

     

输入:测试123我的信件计数程序。

     

输出:t = 5 e = 3 g = 2 n = 2 c = 1 i = 1 p = 1 u = 1 r = 4 o = 3 m   = 2 a = 1 f = 1 l = 1 s = 1 y = 1

我尝试编码这个问题,但没有奏效。这是我的代码:

x

帮助我编写这个问题。因为我不明白如何编码这个问题

2 个答案:

答案 0 :(得分:1)

use strict;
use warnings;

my $input = 'Testing 123 for my letter count program.';
my %seen;
my @order = grep { !$seen{$_}++ } lc($input) =~ /([a-z])/ig;
print "$_ = $seen{$_}\n" for @order;

输出

t = 5
e = 3
s = 1
i = 1
n = 2
g = 2
f = 1
o = 3
r = 4
m = 2
y = 1
l = 1
c = 1
u = 1
p = 1
a = 1

答案 1 :(得分:0)

#!/usr/bin/perl
use strict; use warnings;

my $input = 'Testing 123 for my letter count program.';
my %hash = ();
map { $hash{lc($_)}++ } grep /[a-zA-Z]/, split('', $input);
print "Output: ";
print "$_ = $hash{$_} " for(keys %hash);

<强>输出:

Output: e = 3 y = 1 a = 1 r = 4 s = 1 g = 2 c = 1 n = 2 l = 1 t = 5 i = 1 p = 1 f = 1 m = 2 u = 1 o = 3