Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: improve ugly flow control

by FoxtrotUniform (Prior)
on Sep 19, 2004 at 07:53 UTC ( #392136=note: print w/ replies, xml ) Need Help??


in reply to improve ugly flow control

How about putting a "fail" sentinel at the end of @options and in %hash, such that do_something('fail') is the same as log_failure()? That way, your loop simplifies to:

foreach (@options) { if(exists $hash{$_}) { # always true for $_='fail' &do_something($_); last; } }
Now admittedly, inserting the sentinel into @options and %hash is obnoxious and hackish, but better to have that in your data than in your code....

--
F o x t r o t U n i f o r m
Found a typo in this node? /msg me
% man 3 strfry


Comment on Re: improve ugly flow control
Select or Download Code
Re^2: improve ugly flow control
by Aristotle (Chancellor) on Sep 19, 2004 at 11:07 UTC

    In the same vein, but along a different angle:

    foreach my $i ( 0 .. $#options, 'fail' ) { if( $i eq 'fail' ) { log_failure(); last; } if( exists $hash{ $options[ $i ] } ) { do_something( $options[ $i ] ); last; } }

    Makeshifts last the longest.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://392136]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (2)
As of 2014-07-26 12:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (176 votes), past polls