简体   繁体   English

在 perl 中访问 hash 参考

[英]Accessing hash reference in perl

I am wondering if the following is possible to do in perl.我想知道在 perl 中是否可以执行以下操作。 It will save 40-50 lines of code.它将节省 40-50 行代码。

I have a hash data structure like following:我有一个 hash 数据结构,如下所示:

hash_Ref->{a}->{b}->{c}->{d}->{e}->{'count'}=30

I am wondering is there a way I can do the following:我想知道有没有办法可以做到以下几点:

my $hash_ref_1 = ash_Ref->{a}->{b}->{c}->{d};

and then use:然后使用:

$hash_ref_2->{e}. 

So in summary I want to store hash reference till a point "x" in the hierarchy in a variable and then access the reference which the point "x" points to.所以总而言之,我想将 hash 引用存储到变量中层次结构中的点“x”,然后访问点“x”指向的引用。 I think this is more clear in the example above.我认为这在上面的例子中更清楚。

More details更多细节

I tried couple of things but doesnt seem to work for me.我尝试了几件事,但似乎对我不起作用。 I am copying the code but not all the things I tried to copy hash.我正在复制代码,但不是我试图复制 hash 的所有内容。 Also the output I am getting is something like this我得到的 output 也是这样的

 $VAR1 = {
          'e' => {
                   'count' => 2
                 },
          'c' => {
                   'count' => 2
                 },
          'a' => {
                   'count' => 2
                 },
          'b' => {
                   'count' => 2
                 },
          'd' => {
                   'count' => 2
                 }
        };

where I would expect something like this:我会期待这样的事情:

'a' => { 'count' => 2, 'b' => { 'count' => 2, 'c' => ......} }  

Here's some code I used:这是我使用的一些代码:

use strict;
use Data::Dumper;

my @array1 = ('a','b','c','d','e');
my @array2 = ('a','b','c','d','e');
my $hash;

 build_hash(\@array1);
 build_hash(\@array2);


sub build_hash {

    my @array = @{shift @_};

    my $hash_ref;

    for ( my $i =0 ;  $i < scalar @array ; $i++ ){

        print "$i \t $array[$i] \n";
         if ( exists $hash->{$array[$i]} ){

            $hash->{$array[$i]}->{'count'}++;
        }

        else{

            $hash->{$array[$i]}->{'count'}=1;
        }
    }
    print Dumper($hash);

}

I want to build a hierarchy of hash references based on the elements in the perl in the sequential order and possibly using one loop like I have tried to do in the sample code.我想根据 perl 中的元素按顺序构建 hash 引用的层次结构,并且可能使用一个循环,就像我在示例代码中尝试做的那样。

Thanks!谢谢! -Abhi -阿比

# 'a' => { 'count' => 2, 'b' => { 'count' => 2, 'c' => ......} }

sub build_hash {
    $_[0] ||= {};
    my $hash = shift;
    for (@_) {
        $hash = $hash->{$_} ||= {};
        ++$hash->{count};
    }
}

build_hash($hash, @array1);
build_hash($hash, @array2);

If you wanted to use autovivification, you could write it as follows:如果你想使用自动生存,你可以这样写:

sub build_hash {
    my $p = \shift;
    for (@_) {
        $p = \( $$p->{$_} );
        ++$$p->{count};
    }
}

build_hash($hash, @array1);
build_hash($hash, @array2);

See also: Data::Diver参见: Data::Diver

This should work pretty much like you think it should work.这应该像您认为的那样工作。 As a side note, sometimes when you have hash of hash of hash of...what you really wanted in the first place was one hash with a compund key, like $h{"$a,$b,$c"} instead of $h{$a}{$b}{$c}. As a side note, sometimes when you have hash of hash of hash of...what you really wanted in the first place was one hash with a compund key, like $h{"$a,$b,$c"} instead $h{$a}{$b}{$c}。 Just something to keep in mind.只是要记住的事情。 I don't know if it is applicable here.不知道这里是否适用。

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

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