Just another Perl shrine | |
PerlMonks |
[Answered!] Taking a subset of a hashby Narveson (Chaplain) |
on Oct 30, 2010 at 13:02 UTC ( [id://868463]=perlquestion: print w/replies, xml ) | Need Help?? |
Narveson has asked for the wisdom of the Perl Monks concerning the following question: Update I like both the replies that just came in. Thank you both! I am given a configuration hash reference and three keys of interest. I have to return a hash reference that contains at most the three keys of interest, but omits them if their configured value is false, undefined, or nonexistent. So the simple hash-slicing solutionis not acceptable, because keys of interest with false or undefined values are retained, and I want them not to exist. I have written the following pedestrian solution: which (barring typos - I haven't tested it yet) does what I want done. But it's horribly repetitive. Does anyone know an elegant way to write this? Update I must have momentarily forgotten how to loop. Here's what I meant to do.
The loop still feels kind of clumsy. Any suggestions for improvement?
Back to
Seekers of Perl Wisdom
|
|