在 Perl 中,我应该如何填充这个多级散列?
In Perl, how should I populate this multilevel hash?
我正在尝试寻找 Perl 的最佳方法来填充多级散列,同时考虑到丢失的键。
所以我正在使用这个:
if( !exists $customer_data{$customer_id} ) {
$customer_data{$customer_id} = {};
}
$customer_data{$customer_id}->{$salesman_name} //={};
$customer_data{$customer_id}->{$salesman_name}->{$timestamp} = 1;
这很奇怪,因为我正在使用 exists
和 //=
东西,但我不确定如何正确简洁地编写这段代码。
结果类似于:
'1000' => {
'jsmith' => {
1502121730 => 1,
1512321730 => 1
}
}
参见autovivification in perldoc perlreftut。
$customer_data{$customer_id}{$salesperson_name}{$timestamp} = 1;
足够了。
此外,您可能不想要性别化的变量名称。
我正在尝试寻找 Perl 的最佳方法来填充多级散列,同时考虑到丢失的键。
所以我正在使用这个:
if( !exists $customer_data{$customer_id} ) {
$customer_data{$customer_id} = {};
}
$customer_data{$customer_id}->{$salesman_name} //={};
$customer_data{$customer_id}->{$salesman_name}->{$timestamp} = 1;
这很奇怪,因为我正在使用 exists
和 //=
东西,但我不确定如何正确简洁地编写这段代码。
结果类似于:
'1000' => {
'jsmith' => {
1502121730 => 1,
1512321730 => 1
}
}
参见autovivification in perldoc perlreftut。
$customer_data{$customer_id}{$salesperson_name}{$timestamp} = 1;
足够了。
此外,您可能不想要性别化的变量名称。