在 perl 中访问哈希元素的问题 [英] issue accessing elements of hash in perl
本文介绍了在 perl 中访问哈希元素的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下哈希:
my %releaseMap = {"rel1.2.3" => {
"supporedPorts" => ["lnx86", "lnppc"],
"branch" => ["DEV"],
},
"rel2.4" => {
"supporedPorts" => ["lnx86", "lnppc"],
"branch" => ["DEV"],
}
};
我想在变量 $rel 中获取值 rel1.2.3 和 rel2.4,我想在 $port 中获取值 lnx86 和 lnppc,我想在 $branch 中获取值 DEV.
I want to get the values rel1.2.3 and rel2.4 in the variable $rel, I want to get the values lnx86 and lnppc in $port and I want to get the value DEV in $branch.
我对 perl 中的 Hash 概念不熟悉,我无法弄清楚如何做到这一点.
I am new to the Hash concept in perl, I am not able to figure out how this can be done.
有人可以帮忙吗?谢谢
推荐答案
假设你真的有
my %releaseMap = ("rel1.2.3" => {
"supporedPorts" => ["lnx86", "lnppc"],
"branch" => ["DEV"],
},
"rel2.4" => {
"supporedPorts" => ["lnx86", "lnppc"],
"branch" => ["DEV"],
}
);
你可以使用
for my $rel_id (keys(%releaseMap)) {
my $rel = $releaseMap{$rel_id};
my $ports = $rel->{supporedPorts};
my $branches = $rel->{branch};
say "[$rel_id] ports = @$ports; branches = @$branches";
}
文档:
这篇关于在 perl 中访问哈希元素的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文