[英]Referring to a chain of hash keys in a Perl hash of hashes
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,
},
},
},
...
);
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')
.我可以遍历结构并找到一个特定的键,比如
CID
,然后检索它的值并将“路径”存储在一个数组('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.但我想知道如何使用发现的路径以编程方式设置值。
You can walk up the hash using a placeholder $hashref
:您可以使用占位符
$hashref
哈希:
my $hashref = \%deviceSettings;
$hashref = $hashref->{$_} for qw(BB EUTRA DL PLCI);
$hashref->{CID} = 'My New Path';
use Data::Dump;
dd \%deviceSettings;
Outputs:输出:
{
BB => {
EUTRA => {
DL => { CPC => "NORM", PLCI => { CID => "My New Path" }, ULCPc => "NORM" },
UL => {
REFSig => { DSSHift => 2, GRPHopping => 1, SEQHopping => 1 },
SOFFset => 0,
},
},
},
}
Data::Diver is a module for accessing nested structures using paths. Data::Diver是一个使用路径访问嵌套结构的模块。
use Data::Diver 'DiveVal';
my $device_settings_rf = {};
my @path = ( 'BB', 'EUTRA', 'DL', 'PLCI', 'CID' );
DiveVal( $device_settings_rf, \(@path) ) = 99;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.