#!/usr/bin/perl -w
##Code Begin##############
use strict;
print "方法一:中间数组+keys和values函数法n";
my %hash = ("a"=>1000,"b"=>2000,"c"=>3000);
if (%hash) {
my @k = keys %hash;
my @v = values %hash;
my $count = keys %hash; #得到 3,是指有3 个 key/value 对
print "@k","n";
print "@v","n";
print "$countn";
} else {
print "The %hash is not defined any elment already! n";
};
print "方法二:while+each函数法n";
while ((my $key, my $value) = each %hash){
print "$key => $valuen";
}
print "方法三:foreach+keys函数法n";
foreach (sort keys %hash) {
print "$_ => $hash{$_}n";
# 也可以使用额外的临时变量$key
# foreach $key (sort keys %hash) {
# print “ $key => $hash{key}n” ;
};
##Code End################
print `
sed -n '$!p' $0 |cat -n`;