#!/usr/bin/perl -w
use strict;
use warnings;
my %name=("fred"=>"flintstone",
"barney"=>"rubble",
"wilma"=>"flintstone",
);
my $length=keys %name;# Count the number of hash
print "$length\n";
while(my($k,$v)=each%name)# List all the elements of hash
{
print "$k---->$v\n";
}
my $given_name=<STDIN>;# The Usage of <STDIN>
chomp $given_name;# Delete the "\n"
print "$name{$given_name}\n";
output:
3
barney---->rubble
wilma---->flintstone
fred---->flintstone
fred
flintstone
本文展示了一个使用Perl语言操作哈希(关联数组)的基本示例,包括定义、计数元素数量、遍历以及通过键获取值的过程。
404

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



