punch_card_don has asked for the wisdom of the Perl Monks concerning the following question:
What is the cleanest, most simple, way to do this:
If x='key_1', execute the following block of code for every key in %my_hash. But if x != 'key_1', execute that block of code for just that single key of %my_hash.
For example, this would do it, but it's clunky:
%my_hash = ( key_1 => $value_1, key_2 => $value_2, key_3 => $value_3, key_4 => $value_4, key_5 => $value_5 ); foreach $key (keys %my_hash) { if ($x eq 'key_1' || $key eq $x) { do a bunch of stuff; } }
Thanks
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Loop once on condition 1, many times on cond. 2?
by jethro (Monsignor) on Oct 09, 2008 at 15:08 UTC | |
by punch_card_don (Curate) on Oct 09, 2008 at 15:23 UTC | |
by ikegami (Patriarch) on Oct 09, 2008 at 15:32 UTC | |
by jethro (Monsignor) on Oct 09, 2008 at 15:33 UTC | |
by punch_card_don (Curate) on Oct 09, 2008 at 15:37 UTC | |
by AnomalousMonk (Archbishop) on Oct 09, 2008 at 18:19 UTC | |
by gone2015 (Deacon) on Oct 09, 2008 at 16:57 UTC | |
|
Re: Loop once on condition 1, many times on cond. 2?
by apl (Monsignor) on Oct 09, 2008 at 17:31 UTC | |
by GrandFather (Saint) on Oct 09, 2008 at 20:50 UTC |