I can slice kes/values as next:
$item->%{ @cols }
But if some column does not exist at $item It will be created at resulting hash.
Can I slice only defined values?
I can slice kes/values as next:
$item->%{ @cols }
But if some column does not exist at $item It will be created at resulting hash.
Can I slice only defined values?
On
Use
$item->%{ grep { exists($item->{$_}) } @cols }
or
do { $item->%{ @cols } }
Indexing/slicing a hash does not add elements to it.
my @cols = qw( a b c );
my $item = { };
say 0+%$item; # 0
my @kvs = $item->%{ @cols };
say 0+%$item; # 0 ok
Except when it's used as an lvalue (assignable value, such as when on the left-hand side of =).
my @cols = qw( a b c );
my $item = { };
say 0+%$item; # 0
1 for $item->%{ @cols };
say 0+%$item; # 3 XXX
You could filter out the keys of elements that don't exist.
my @cols = qw( a b c );
my $item = { };
say 0+%$item; # 0
1 for $item->%{ grep { exists($item->{$_}) } @cols };
say 0+%$item; # 0 ok
But the simple solution is to not use it as an lvalue.
my @cols = qw( a b c );
my $item = { };
say 0+%$item; # 0
1 for do { $item->%{ @cols } };
say 0+%$item; # 0 ok
You can check whether they exist.
should do the job slicing only the existing values.
Anyway - simply accessing these values should NOT autovivify them. Only if you Pass these values as parameters to some function and they are implicetly aliased there, they are autovivified.
Only the last print will generate the:
indicating that b got autovivified.