in reply to Splitting an array to populate hash
Perhaps you should use an anonymous array for a "key", instead of a list. You then no longer need to wrap each pair in an anonymous array. The resulting code would look more Perlish, IMO.
Now it's just a matter of defining expand_keys():my %hash = expand_keys( ['key1', 'key2', 'key3'] => 'Value for key1, key2, and key3', [qw( key4 key5 key6) ] => 'Value for key4, key5, key6', 'simple key' => 'Value for simple key' );
You could populate a hash instead of pushing the data onto an array, in the sub, I suppose. I think it might be a tiny bit slower, because you would then be building two hashes, instead of just one (and an array).sub expand_keys { my @result; while(@_) { my $key = shift; my $value = shift; if(ref $key) { push @result, $_, $value foreach @$key; } else { push @result, $key, $value; } } return @result; }
|
|---|