in reply to Re^2: get solution as short as possible
in thread get solution as short as possible
second version avoids double using "default_option_" string but, in my opinion, is less straightforward than my original.
Comparewith originalfor ( grep /^default_option_/, keys %def ) { $s{ (split /_/, $_, 2)[-1] } = $def{ $_ }; }
Second case more distinctly highlights idea to use the subpart of key string as a new key. I wished to have something likeforeach (map { /^default_option_(.*)/ ? $1 : () } keys %def) { $s{"option_$_"} = $def{"default_option_$_"}; }
or even more shortfor (grep /^default_option_(.*)/, keys %def) { $s{"option_$1"} = $def{$_}; }
$s{"option_$1"} = $def{$_} for (grep /^default_option_(.*)/, keys %def +);
Of course the last 2 solutions are incorrect. But if some monk knows how to achieve such level of laconism and clearness in one that would be a great pattern.
Anyway I like your solutions for custom, rare cases. But I need a nice looking, straightforward and short version for everyday use. Of course if it exists in Perl 5 universe.
Another problem is that your last solution truncates hash if it has data. It's not appropriate for me. Though I'm not sure if moritz's version does this.
FYI: I've just registered. And this is my post
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: get solution as short as possible
by parv (Parson) on Aug 17, 2011 at 18:20 UTC |