在 perl 中访问哈希引用
我想知道是否可以在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果你想使用自动激活,你可以这样写:
另请参阅: Data::Diver
If you wanted to use autovivification, you could write it as follows:
See also: Data::Diver
这应该像你想象的那样工作。作为旁注,有时当你有......的哈希值的哈希值时,你首先真正想要的是带有复合键的哈希值,例如 $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.