in reply to Re: ugly nested if's
in thread ugly nested if's
@lookup_hash{@list2} = undef;It'll do exactly what you want, but only in this particular case. It exhibits a lack of elegance: it looks like it's doing something other than what it's really doing. Let me demonstrate what I mean, by assigning a different value:
Result:my @list2 = qw(one two three); my %lookup_hash; @lookup_hash{@list2} = 'foo'; use Data::Dumper; print Dumper \%lookup_hash;
If you were expecting to see "foo" for every value, think again. You are assigning an explicit value (in your code, undef) to the first hash element in the slice, and an implicit undef to all the others.$VAR1 = { 'three' => undef, 'one' => 'foo', 'two' => undef };
As for better idioms (IMO)... In this particular case, I'd assign an implied undef for all hash elements, no exceptions:
In order to assign the same explicit value to all:@lookup_hash{@list2} = ();
@lookup_hash{@list2} = ('foo') x @list2;
--
Thank you. I'll get off my soap box now.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re(2): ugly nested if's (broken idiom)
by dragonchild (Archbishop) on Apr 23, 2004 at 11:36 UTC |