在散列的 Perl 散列中引用散列键链 [英] Referring to a chain of hash keys in a Perl hash of hashes
本文介绍了在散列的 Perl 散列中引用散列键链的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个散列的散列存储数据
I have a hash of hashes storing data like so
our %deviceSettings = (
BB => {
EUTRA => {
DL => { CPC => "NORM", PLCI => { CID => 88 }, ULCPc => "NORM" },
UL => {
REFSig => { DSSHift => 2, GRPHopping => 1, SEQHopping => 1 },
SOFFset => 0,
},
},
},
...
);
我可以遍历结构并找到一个特定的键,比如 CID
,然后检索它的值并将路径"存储在一个数组中 ('BB', 'EUTRA', 'DL', 'PLCI')
.
I can walk the structure and find a particular key, say CID
, and retrieve its value and store the 'path' in an array ('BB', 'EUTRA', 'DL', 'PLCI')
.
我也可以像这样显式设置一个值
I can also explicitly set a value, like this
$deviceSettings_ref->{BB}{EUTRA}{DL}{PLCI}{CID} = 99
但我想知道如何使用发现的路径以编程方式设置值.
But I want to know how to set a value programatically using a discovered path.
推荐答案
您可以使用占位符$hashref
:
my $hashref = \%deviceSettings;
$hashref = $hashref->{$_} for qw(BB EUTRA DL PLCI);
$hashref->{CID} = 'My New Path';
use Data::Dump;
dd \%deviceSettings;
输出:
{
BB => {
EUTRA => {
DL => { CPC => "NORM", PLCI => { CID => "My New Path" }, ULCPc => "NORM" },
UL => {
REFSig => { DSSHift => 2, GRPHopping => 1, SEQHopping => 1 },
SOFFset => 0,
},
},
},
}
这篇关于在散列的 Perl 散列中引用散列键链的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文