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

如何在perl中找出散列引用点的位置?,perl,hashmap,Perl,Hashmap,我有一个脚本,它调用一个函数,通过将散列转换为引用递归地遍历散列,并始终向引用添加下一个键。我想知道是否有可能找到散列引用指向的位置。这不是我的脚本,只是一个示例 #!/usr/bin/perl -w use strict; use Data::Dumper; my %hoh = ( flintstones => { husband => "fred", pal => "barney", }, jets

我有一个脚本,它调用一个函数,通过将散列转换为引用递归地遍历散列,并始终向引用添加下一个键。我想知道是否有可能找到散列引用指向的位置。这不是我的脚本,只是一个示例

#!/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
我知道引用指向内存中的一个位置,因此如果哈希引用只指向数据的位置,而不在原始哈希中存储所需的键,那么这是不可能的。我不想遍历原始散列并找到值的位置,关键是,如果有一种简单的方法可以做到这一点。如果没有,我可以跟踪键,散列引用指向哪里,我只是好奇是否可以跳过它

如何在perl中找出散列引用点的位置

$hoh_ref
不是散列引用(或任何类型的引用)

与相同

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\u 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      |  |
                                           |               +-----------+  |
                                           +------------------------------+
我不想遍历原始散列并找到值所在的位置


太糟糕了。

我认为那条路就是疯狂。自己跟踪键。@simbabque是的,我可能会跟踪键,我很好奇一些perl向导是否知道一种方法,这或多或少是用蛮力实现的。关于“我知道哈希指向内存中的一个位置”,参考点。散列是一种数据结构(一种关联数组)。@ikegami更正,我相应地编辑了这个问题。要得到什么键?好吧,只有
flintstones
my$key=“flintstones”\%hash$ref->{key1}。。。因此,仅使用$ref,例如,不知道散列,您可以知道使用了哪些键来获取该值吗?我现在确信,不,你不能。希望你现在明白了。如果我不清楚,很抱歉。没有使用键来获取该值。可能使用了赋值,但您没有说明它是如何获得其值的。