1.[7]写一个程序,提示用户输入given name(名),并给出其对应的family name(姓)。使用你知道的人名,或者表6-1
(如果你在计算机上花了太多时间,以致什么人都不认识):
表6-1 样本数据
输入输出
fred flintstone
barney rubble
wilma flintstone
2.[15]写一个程序,读入一串单词(一个单词一行)◆,输出每一个单词出现的次数。(提示:如果某个作为数字使用值是
undefined 的,会自动将它转换为0。)如果输入单词为fred, barney, dino, wilma, fred(在不同行中),则输出的fred 将为
3。作为额外的练习,可以将输出的单词按照ASCII 排序。
1、#!/usr/bin/perl -w
use strict;
my %name=(
"fred" => "flintstone",
"barney"=>"rubble",
"wilma"=>"flintstone",
);
print "Please enter given name:";
chomp ( my $given_name=<STDIN>);
if (exists $name{$given_name}){
print "The $given_name/'s family name is: $name{$given_name}./n";
}
2-3、#!/usr/bin/perl -w
use strict;
my %hash;
while(<>){
chomp;
$hash{$_}++;
}
#print %hash;
foreach (sort keys %hash){
print "$_ => $hash{$_}./n";
}
本文提供两个Perl程序实例:一是查询姓名对应的姓氏;二是统计文本中各单词出现频率,并按ASCII顺序排列输出。通过实际操作加深对Perl哈希表及文件输入输出的理解。
&spm=1001.2101.3001.5002&articleId=4668533&d=1&t=3&u=83ba1612ddca4134b1afb5e3000ffda1)
1325

被折叠的 条评论
为什么被折叠?



