简体   繁体   English

问题在 perl 中访问 hash 的元素

[英]issue accessing elements of hash in perl

I have the below hash:我有以下 hash:

my %releaseMap = {"rel1.2.3" => {
                                    "supporedPorts" => ["lnx86", "lnppc"],
                                    "branch"        => ["DEV"],
                                },

                  "rel2.4" =>   {
                                    "supporedPorts" => ["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.我想在变量 $rel 中获取值 rel1.2.3 和 rel2.4,我想在 $port 中获取值 lnx86 和 lnppc,我想在 $branch 中获取值 DEV。

I am new to the Hash concept in perl, I am not able to figure out how this can be done.我是 perl 中 Hash 概念的新手,我无法弄清楚如何做到这一点。

Can someone please help.有人可以帮忙吗。 Thanks谢谢

Assuming you really have假设你真的有

my %releaseMap = ("rel1.2.3" => {
                                    "supporedPorts" => ["lnx86", "lnppc"],
                                    "branch"        => ["DEV"],
                                },

                  "rel2.4" =>   {
                                    "supporedPorts" => ["lnx86", "lnppc"],
                                    "branch"        => ["DEV"],
                                }
                 );

You can use您可以使用

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";
}

Docs:文件:

A hash is initialised from a list: A hash 从列表中初始化:

my %hash = ( key => value, ... );

A hash reference is initialised using the anonymous hash constructor:使用匿名 hash 构造函数初始化 hash引用

my $hash_ref = { key => value, ... };

You are initialising your hash with an anonymous hash constructor.您正在使用匿名 hash 构造函数初始化 hash。 This does not work as you expect it.这不像您预期的那样工作。 You end up with a hash containing a single key (which is the stringification of the anonymous hash reference:您最终会得到一个包含单个键的 hash (这是匿名 hash 参考的字符串化:

my %hash = { key => value, ... };

If you change your code to this:如果您将代码更改为此:

my %releaseMap = ("rel1.2.3" => {
                                    "supporedPorts" => ["lnx86", "lnppc"],
                                    "branch"        => ["DEV"],
                                },

                  "rel2.4" =>   {
                                    "supporedPorts" => ["lnx86", "lnppc"],
                                    "branch"        => ["DEV"],
                                }
                 );

Then things get easier.然后事情变得更容易了。 You can start with:你可以从:

foreach my $key (keys %releaseMap) {
  say $key;
}

Also, add use strict and use warnings to your code.此外,在代码中添加use strictuse warnings They will catch a lot of problems for you.他们会为你解决很多问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM