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.

Compare
for ( grep /^default_option_/, keys %def ) { $s{ (split /_/, $_, 2)[-1] } = $def{ $_ }; }
with original
foreach (map { /^default_option_(.*)/ ? $1 : () } keys %def) { $s{"option_$_"} = $def{"default_option_$_"}; }
Second case more distinctly highlights idea to use the subpart of key string as a new key. I wished to have something like
for (grep /^default_option_(.*)/, keys %def) { $s{"option_$1"} = $def{$_}; }
or even more short
$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
    A(...) 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.

    If you are objecting to destruction of %s, then mortiz's version does preserve the previous elements.

    As there is no getting around search for interesting keys and generation of second key list, so just use ...

    sub simple_fill { my ( $save , %def ) = @_; for ( keys %def ) { my ( $opt ) = ( $_ =~ m/^ default_ (option_.+) $/x ) or next; $save->{ $opt } = $def{ $_ }; } return; }

    And then there is use of map|grep in void context for side effects, which some may not regard as "a nice looking, straightforward (...) version for everyday use".