my %myHash = (
key1 => {
test1 => 1,
test2 => 2,
},
key2 => {
test1 => 3,
test2 => 4,
},
);
my $myRef = $myHash{ "key". ((~~keys %myHash) + 1) } //= {
test1 => 5,
test2 => 6,
};
幽默我并假设上述内容实际上是实用的。我如何通过引用删除这个新创建的密钥?
delete $myRef;
显然行不通
编辑: 所以从 zostay 我有以下...
sub deleteRef {
my ( $hash_var, $hash_ref ) = @_;
for ( keys %$hash_var ) {
delete $hash_var->{$_} if ($hash_var->{$_} == $hash_ref);
}
}
用法:
deleteRef(\%myHash, $myRef);
怎么样?还是不推荐?