Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Last Update: This is wrong. See below for details.

Here's a somewhat exotic construction that I think reads well. Assumes that do_something() returns true on success.

log_failure() unless grep { exists($hash{$_}) && do_something($_) .. 1 } @options;
or maybe better,
grep { exists($hash{$_}) && do_something($_) .. 1 } @options or log_failure();
Untested, but I think it's nearly right.

The flipflop is true for zero or one keys and the initiating condition is not evaluated again after it is first true.

Update: Tested now, and it's not right. Trying to fix it.

Another Update: Is this a bug in perl's flipflop op? In perl-5.8.4

$ perl -Mstrict -we'my $s=1;for(qw/foo bar baz/){ print if /b/..$s }' barbaz$ $ perl -Mstrict -we'my $s=1;for(qw/foo bar baz/){ print if $s../b/ }' $
I'm using a variable $s there because bart++ tells me that flipflop can play games with $. if bare integers are presented to it.

Final Update: There is no bug, except in my logic. Changing the right hand side of the flipflop to match anything (//) instead of fooling with constants, I find by printing the sequence numbers that the flipflop switches off ok, but switches on again next time around.

$ perl -e'for (qw/foo bar baz blah/){print int scalar(/b/..//)}' 0111$
Silly mistake on my part.

Here is a version that works, but it still needs that pesky $success variable I was trying to get rid of.

{ my $success; grep { ! $success && exists($hash{$_}) && do_something($_) .. ($success = 1) } @options or log_failure(); }
$success must be checked first to short-circuit the rest of the processing.

After Compline,
Zaxo


In reply to Re: improve ugly flow control by Zaxo
in thread improve ugly flow control by perrin

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2024-04-18 00:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found