Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
Hello guys
Let there be a hash
%def = ( default_option_use_log => 1, default_option_read_conf => 1, default_option_run_debug => "false", default_output_path => "/tmp/vvv", default_name => "stat_", default_option_save_state => "true" );
We need to fill a new hash with keys based on filtered and changed keys of %def hash. Consider the following code:
foreach (map { /^default_option_(.*)/ ? $1 : () } keys %def) { $s{"option_$_"} = $def{"default_option_$_"}; } foreach (keys %s) { print "\%s: $_ => $s{$_}\n"; }
Is there a way to shorten the 'foreach' block or may be use different approach instead of 'foreach'. It would be great to eliminate double using 'default_option_' string, also '? : ()' condition looks not perfect. I need a nice-looking, laconic, as much Perl-ish as possible, solution.
Thanks
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: get solution as short as possible
by moritz (Cardinal) on Aug 17, 2011 at 08:51 UTC | |
by throop (Chaplain) on Aug 17, 2011 at 15:23 UTC | |
by Anonymous Monk on Aug 17, 2011 at 11:34 UTC | |
by dexahex (Novice) on Aug 17, 2011 at 13:20 UTC | |
by parv (Parson) on Aug 17, 2011 at 18:20 UTC | |
|
Re: get solution as short as possible
by ww (Archbishop) on Aug 17, 2011 at 10:51 UTC |