《Perl语言入门》第四版习题(6)

来源:互联网 发布:三星在线升级软件 编辑:程序博客网 时间:2024/05/18 00:42

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";
}