perl 取出多个hash的值,可以用循环:
下面的代码是遍历hash:
while(($key, $value) = each(%HASH)) {
# do something with $key and $value
}
#-----------------------------
foreach $key (keys %HASH) {
$value = $HASH{$key};
# do something with $key and $value
}
#-----------------------------
# %food_color per the introduction
while(($food, $color) = each(%food_color)) {
print "$food is $color.\n";
}
# Banana is yellow.
#
# Apple is red.
#
# Carrot is orange.
#
# Lemon is yellow.
foreach $food (keys %food_color) {
my $color = $food_color{$food};
print "$food is $color.\n";
}
如果觉得我的回答对您有用,请随意打赏。你的支持将鼓励我继续创作!