在 perl 中访问哈希引用

发布于 2024-11-11 04:17:11 字数 1715 浏览 3 评论 0原文

我想知道是否可以在 perl 中执行以下操作。它将节省 40-50 行代码。

我有一个如下的哈希数据结构:

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

我想知道有没有一种方法可以执行以下操作:

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

然后使用:

$hash_ref_2->{e}. 

所以总而言之,我想将哈希引用存储到变量中的层次结构中的点“x”,然后访问点“x”指向的参考。我认为这一点在上面的例子中更清楚。

更多细节

我尝试了几件事,但似乎对我不起作用。我正在复制代码,但不是我尝试复制哈希的所有内容。另外,我得到的输出是这样的,

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

我期望这样的输出:

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

这是我使用的一些代码:

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);

}

我想根据 perl 中的元素按顺序构建一个哈希引用的层次结构,并可能使用一个循环,例如我已经尝试在示例代码中执行此操作。

谢谢! -阿比

I am wondering if the following is possible to do in perl. It will save 40-50 lines of code.

I have a hash data structure like following:

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. 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. Also the output I am getting is something like this

 $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.

Thanks!
-Abhi

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

坦然微笑 2024-11-18 04:17:11
# '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);

如果你想使用自动激活,你可以这样写:

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

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

另请参阅: Data::Diver

# '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

殤城〤 2024-11-18 04:17:11

这应该像你想象的那样工作。作为旁注,有时当你有......的哈希值的哈希值时,你首先真正想要的是带有复合键的哈希值,例如 $h{"$a,$b,$c"} $h{$a}{$b}{$c}。只是要记住一些事情。不知道这里是否适用。

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}. Just something to keep in mind. I don't know if it is applicable here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文