使用空字符串正确拆分输入字符串
//
@Array
又一次,太空?那会导致你
%numbers
存在
(1 => 1)
.
作为
split //
use warnings;
use strict;
use feature 'say';
while (1) {
print "Enter the number: ";
my $num = <STDIN>;
chomp $num;
if ($num =~ /[^0-9]/) {
say "Non-digit(s) in input $num. Please try again.";
next;
}
my @digits = split //, $num; #/
my %freq;
++$freq{$_} for @digits;
for (sort keys %freq) {
say "Num are $freq{$_} with ", (sprintf "%.2f%%", ($freq{$_}/@digits)*100)
}
}
112219992221474774
输出为
Num 1 are 4 with 22.22%
Num 2 are 5 with 27.78%
Num 4 are 3 with 16.67%
Num 7 are 3 with 16.67%
Num 9 are 3 with 16.67%