【问题标题】:issue accessing elements of hash in perl在 perl 中访问哈希元素的问题
【发布时间】:2020-06-02 06:13:32
【问题描述】:

我有以下哈希:

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。

我是 perl 中 Hash 概念的新手,我无法弄清楚如何做到这一点。

有人可以帮忙吗?谢谢

【问题讨论】:

  • 如果你真的有my %releaseMap = { ... };,你有一个带有单个元素的散列,其键类似于HASH(0xFFFFFFFF),其值为undef。该代码还发出警告。你的意思是my %releaseMap = ( ... );?

标签: perl


【解决方案1】:

假设你真的有

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

文档:

【讨论】:

    【解决方案2】:

    哈希是从列表中初始化的:

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

    使用匿名哈希构造函数初始化哈希reference:

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

    您正在使用匿名哈希构造函数初始化您的哈希。这不像您预期​​的那样工作。您最终会得到一个包含单个键的哈希(这是匿名哈希引用的字符串化:

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

    如果您将代码更改为:

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

    然后事情变得更容易了。你可以从:

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

    另外,将use strict 和use warnings 添加到您的代码中。他们会为你解决很多问题。

    【讨论】:

    • 感谢大家的回复。它真的很有帮助。我使用了以下代码:
    • 我的 %releaseMap = ("rel1.2.3" => { "supportedPorts" => ["lnx86"], "branch" => ["DEV"], }, "rel2.4" => { "supportedPorts" => ["lnx86", "lnppc"], "branch" => ["DEV"], });
    • ); foreach 我的 $release (keys (%releaseMap)) { my $rel = $releaseMap{$release};我的 $port = $rel->{supportedPorts};我的 $branch = $rel->{branch}; print "$release\n port = @$port\n branch = $branch\n";它给了我以下输出: rel2.4 port = lnx86 lnppc branch = ISR rel1.2.3 port = lnx86 branch = ISR
    • 我还有一个问题。我可以使用上面的代码在变量 $release1 中将 rel1.2.3 和 rel2.4 的值作为 rel123 和 rel24 获得吗?有没有办法做到这一点?请帮忙。
    • @PaulIT:如果有人回答了您最初的问题,那么您应该接受他们的回答。如果您现在还有其他问题,那么您应该单独提出这些问题。
    猜你喜欢
    • 2017-07-05
    • 2013-11-01
    • 1970-01-01
    • 2021-08-01
    • 1970-01-01
    • 2011-09-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多