Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Comment on

( #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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others imbibing at the Monastery: (7)
    As of 2014-07-26 05:15 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite superfluous repetitious redundant duplicative phrase is:









      Results (175 votes), past polls