Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

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
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?

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2015-11-30 03:47 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (757 votes), past polls