in reply to Accidentally creating hash elements with grep { defined } on a hash slice
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;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Accidentally creating hash elements with grep { defined } on a hash slice
by ccn (Vicar) on Nov 04, 2008 at 11:35 UTC | |
by ikegami (Patriarch) on Nov 04, 2008 at 11:39 UTC | |
by wol (Hermit) on Nov 04, 2008 at 13:51 UTC | |
by ikegami (Patriarch) on Nov 04, 2008 at 15:21 UTC | |
by ferment (Novice) on Nov 04, 2008 at 14:28 UTC |