Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
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

Replies are listed 'Best First'.
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?
[Corion]: Whee! Germanys Grand Central Airport (for Berlin, re-scheduled for the thrid or fourth time, to open now in 2018) has just fired their technical lead

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (11)
As of 2017-02-23 11:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?






    Results (345 votes). Check out past polls.