Can you tell me why some mysterious key can appear from (keys %stat)?
Because the code you did not show somehow created them.
Perhaps you did $stat{510}{k1}
? Keep in mind that
$stat{510}{k1}
is short for
$stat{510}->{k1}
and
$x->{...}
does
( $x //= {} )->{...}
so
$stat{510}{k1}
does
( $stat{510} //= {} )->{k1}
Notice how this assigns to $stat{510}
?
Using map
as a for
loop is frowned upon.
map { delete $stat{$_} if(!defined $stat{$_}{k1}) } keys %stat;
is better written as
delete $stat{$_} for grep !defined($stat{$_}{k1}), keys %stat;
or even
delete @stat{ grep !defined($stat{$_}{k1}), keys %stat };