如何找出哈希引用在perl中指向的位置?

时间:2018-07-18 14:37:07

标签: perl hashmap

我有一个脚本,该脚本调用一个函数以将其转换为引用,并始终将下一个键添加到引用中,以递归地遍历所有哈希。我想知道是否有可能找出哈希引用指向的位置。这不是我的脚本,只是一个例子。

#!/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 个答案:

答案 0 :(得分:-1)

  

如何找出perl中哈希引用的位置?

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

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

相同
my $hoh_ref = "fred";

表示将创建标量$hoh_ref并将其值设置为字符串fred。在这两种情况下,(错误命名的)$hoh_ref%hoh之间绝对没有关联,只是HoH的值之一恰好等于$hoh_ref的值。

+- %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_ref之一相等的值。

  

我不想遍历原始哈希并找到值所在的地方

太糟糕了。