怎么样从哈希中一次取出多个值

请先 登录 后评论

最佳答案 2019-01-04 12:14



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";
}
请先 登录 后评论

其它 0 个回答

  • 1 关注
  • 0 收藏,3220 浏览
  • 小学生 提出于 2018-08-20 18:41

相似问题