如何使用带有散列引用的foreach? [英] How to use foreach with a hash reference?

查看:117
本文介绍了如何使用带有散列引用的foreach?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码

foreach my $key (keys %ad_grp) {

    # Do something
}

有效.

如果我没有%ad_grp,但是对哈希的引用$ad_grp_ref,会是什么样子?

How would the same look like, if I don't have %ad_grp, but a reference, $ad_grp_ref, to the hash?

推荐答案

foreach my $key (keys %$ad_grp_ref) {
    ...
}

Perl::Critic和daxim推荐样式

Perl::Critic and daxim recommend the style

foreach my $key (keys %{ $ad_grp_ref }) {
    ...
}

出于对可读性和维护性的考虑(因此,当您需要使用%{ $ad_grp_obj[3]->get_ref() }而不是%{ $ad_grp_ref }时,您无需考虑要更改的内容)

out of concerns for readability and maintenance (so that you don't need to think hard about what to change when you need to use %{ $ad_grp_obj[3]->get_ref() } instead of %{ $ad_grp_ref })

这篇关于如何使用带有散列引用的foreach?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆