Why does testing for definition of a value create a key?
Bad question. It's not defined that creates it.
>perl -MData::Dumper -e"grep { 0 } @h{qw(a b)}; print Dumper \%h;" $VAR1 = { 'a' => undef, 'b' => undef };
grep aliases $_ to the element being tested. That means is needs something to alias to. That causes the element to be created.
Is there a way around the key creation?
Yes, pass the keys to grep, not the values.
my @defined_values = map $benchmarks{$_}, grep defined($benchmarks{$_}), @BASELINES;
or maybe you really want
my @defined_values = map $benchmarks{$_}, grep exists($benchmarks{$_}), @BASELINES;
In reply to Re: Accidentally creating hash elements with grep { defined } on a hash slice
by ikegami
in thread Accidentally creating hash elements with grep { defined } on a hash slice
by ferment
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |