in reply to Re^4: Loop once on condition 1, many times on cond. 2?
in thread Loop once on condition 1, many times on cond. 2?
Further, because the above will loop over all keys if $x eq 'key_1' is true even if 'key_1' does not exist in the hash (which may or may not be what you want: Your Logic May Vary), perhaps re-order list generation:my @keys_to_process = $x eq 'key_1' ? keys %my_hash : exists $my_hash{$x} ? ($x) : () # default - nothing to do ; foreach $key (@keys_to_process) { do_something_with($key); }
my @keys_to_process = ! exists $my_hash{$x} ? () : $x eq 'key_1' ? keys %my_hash : ($x) ;
|
|---|