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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值