Use Examples Regarding Hash in Perl

1.要求用户输入人名, 打印出对应的item.

1
2
3
4
5
6
7
8
9
my %last_name = qw{
    fred flintstone
    barney rubble
    wilma flintstone
};

print "please enter a first name: ";
chomp(my $name = <STDIN>);
print "that's $name $last_name{$name}.\n";

这是一个很典型的标准用法了。

2.要求用户进行几行输入,然后统计频数.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
use strict;
use warnings;
use utf8;

my (@words, %count, $word);
chomp(@words = <STDIN>);

foreach $word (@words){
    $count{$word} += 1;
}

foreach $word (keys %count){
    print "$word was seen $count{$word} times.\n";
}