我正在填充这样的数据结构:
push @{$AvailTrackLocsTop{$VLayerName}}, $CurrentTrackLoc;
Run Code Online (Sandbox Code Playgroud)
其中$ VLayerName是一个字符串,例如m1,m2,m3等,而$ CurrentTrackLoc只是一个十进制数字。如果我在完全填充哈希之后使用Data :: Dumper打印哈希的内容,它将显示我期望的结果,例如:
$VAR1 = {
'm11' => [
'0.228',
'0.316',
'0.402',
'0.576',
'0.750',
'569.458',
'569.544',
'569.718',
'569.892'
]
};
Run Code Online (Sandbox Code Playgroud)
现在,我需要有效地拼接存储的十进制数字列表。我可以这样删除条目:
for (my $i = $c; $i <= $endc; $i++) {
delete $AvailTrackLocsTop{$VLayerName}->[$i];
}
Run Code Online (Sandbox Code Playgroud)
正如预期的那样,结果是一堆“ undef”条目,这些条目曾经存在数字,例如:
$VAR1 = {
'm11' => [
undef,
undef,
undef,
undef,
'0.750',
'569.458',
'569.544',
'569.718',
'569.892'
]
};
Run Code Online (Sandbox Code Playgroud)
但是,如何清除undef条目,以便我看到类似的内容?
$VAR1 = {
'm11' => [
'0.750',
'569.458',
'569.544',
'569.718',
'569.892'
]
};
Run Code Online (Sandbox Code Playgroud)
重要的是要注意,删除操作可以在数组中的任何位置进行,例如,索引33和100中的索引99。在哈希结构的上下文之外很容易拼接数组,但是在嵌入数组时,我很努力地操作数组在一个大哈希中。
首先,我要从删除文档中注意:
WARNING: Calling delete on array values is strongly discouraged. The notion of deleting or checking the existence of Perl array elements is not conceptually coherent, and can lead to surprising behavior.
Run Code Online (Sandbox Code Playgroud)
将数组元素设置为undef的正确方法是使用undef函数(或仅将undef分配给它)。
要删除元素,可以使用splice函数,它在嵌套arrayrefs上的作用方式与在普通数组上相同,只需要像对一样取消引用它push。
splice @{$AvailTrackLocsTop{$VLayerName}}, $c, $endc - $c + 1;
Run Code Online (Sandbox Code Playgroud)