代码之家  ›  专栏  ›  技术社区  ›  TheAdam122

如何在Perl中找出散列引用的位置?

  •  0
  • TheAdam122  · 技术社区  · 7 年前

    我有一个脚本,它调用一个函数,通过将散列转换为引用递归地遍历散列,并且总是向引用添加下一个键。我想知道是否可以找到哈希引用指向的地方。这不是我的脚本,只是一个例子。

    #!/usr/bin/perl -w
    
    use strict;
    use Data::Dumper;
    
    my %hoh = (
        flintstones => {
            husband   => "fred",
            pal       => "barney",
        },
        jetsons => {
            husband   => "george",
            wife      => "jane",
            "his boy" => "elroy",
        },
        simpsons => {
            husband   => "homer",
            wife      => "marge",
            kid       => "bart",
        },
    );
    
    print Dumper \%hoh;
    
    #my hash has way more levels and not all levels have the same depth
    #my scrypt does what is in the next 3 lines, just through more levels
    
    my $hoh_ref=\%hoh;
    $hoh_ref=$hoh_ref->{flintstones};
    $hoh_ref=$hoh_ref->{husband};
    
    #it would be nice to know where a hash reference points, like the following example:
    #sub findkeys{???}
    #findkeys($hoh_ref);
    #should return '$hoh{flintstones}{husband}' the syntax can be different
    
    print "$hoh_ref\n";
    print "$hoh{flintstones}{husband}\n";     #same as last line, just to demonstrate what it looks like using keys
    

    我知道引用指向内存中的一个位置,所以如果散列引用只指向数据的位置,而不存储原始散列中所需的键,则不可能。我不想循环遍历原始散列并找到值所在的位置,关键是,如果有一种简单的方法可以做到这一点的话。如果没有,我可以跟踪键,哈希引用指向的地方,我只是好奇是否可以跳过它。

    1 回复  |  直到 7 年前
        1
  •  -1
  •   ikegami Gilles Quénot    7 年前

    如何在Perl中找出散列引用的位置?

    $hoh_ref 不是哈希引用(或任何类型的引用)。

    my $hoh_ref = \%hoh;
    $hoh_ref = $hoh_ref->{flintstones};
    $hoh_ref = $hoh_ref->{husband};
    

    与相同

    my $hoh_ref = "fred";
    

    也就是说它创建了标量 $HOH参考 并将其值设置为字符串 fred . 在这两种情况下,两者之间绝对没有关联(名称错误) $HOH参考 %hoh 但是,hoh的一个值恰好等于 $HOH参考 .

    +- %hoh -----------------------+           +- Anon hash ------------------+
    |               +-----------+  |     +---->|               +-----------+  |
    |  flintstones: | Ref      ----------+     |  husband:     | fred      |  |
    |               +-----------+  |           |               +-----------+  |
    |  jetsons:     | Ref      ------------+   |  pal:         | barney    |  |
    |               +-----------+  |       |   |               +-----------+  |
    |  simpsons:    | Ref      --------+   |   +------------------------------+
    |               +-----------+  |   |   |
    +------------------------------+   |   |   +- Anon hash ------------------+
                                       |   +-->|               +-----------+  |
    +- $hoh_ref -------------------+   |       |  husband:     | george    |  |
    | fred                         |   |       |               +-----------+  |
    +------------------------------+   |       |  wife:        | jane      |  |
                                       |       |               +-----------+  |
                                       |       |  his boy:     | elroy     |  |
                                       |       |               +-----------+  |
                                       |       +------------------------------+
                                       |
                                       |       +- Anon hash ------------------+
                                       +------>|               +-----------+  |
                                               |  husband:     | homer     |  |
                                               |               +-----------+  |
                                               |  wife:        | marge     |  |
                                               |               +-----------+  |
                                               |  kid:         | bart      |  |
                                               |               +-----------+  |
                                               +------------------------------+
    

    所以,充其量,您可以迭代 %HOH 查找等于 $HOH参考 .

    我不想循环遍历原始哈希并找到值的位置

    太糟糕了。