Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Back step in foreach loop

by rosenrosen (Initiate)
on Nov 10, 2008 at 23:13 UTC ( [id://722746]=perlquestion: print w/replies, xml ) Need Help??

rosenrosen has asked for the wisdom of the Perl Monks concerning the following question:

I have a foreach loop iterating over an array and if a certain test fails for an item I'd like to sleep and then try it again. Is there an easy way to re-enter the loop without grabbing the next item from the array?
foreach $item (@array) { if (some_test($item)) { print "Failure: $item\n"; #inset tricky back step thing here sleep 10; } else { print "Success: $item\n"; } }

Replies are listed 'Best First'.
Re: Back step in foreach loop
by Limbic~Region (Chancellor) on Nov 10, 2008 at 23:23 UTC
Re: Back step in foreach loop
by duckyd (Hermit) on Nov 11, 2008 at 01:43 UTC
    I doubt that you want to open the door to having the test fail endlessly, and sleeping and retrying over and over. My guess is that you want to retry up to some maximum number of times, and complain very loudly if it doesn't pass within the allowed number of retries, I.E.:
    my $max_tries = 5; foreach $item (@array) { my $tries = 0; while(some_test($item) and $tries++ < $max_tries) { print "Failure: $item, trying again after sleep\n"; sleep 10; } if( $tries == $max_tries){ die "Failed $tries times, aborting"; }else{ print "Success: $item\n"; } }
    (untested)

      Personally, I don't like having to perform the same test ($tries == $max_tries) twice.

      my $max_tries = 5; foreach $item (@array) { my $tries = 0; while (!some_test($item)) { die "Failed $tries times, aborting" if ++$tries == $max_tries; print "Failure: $item, trying again after sleep\n"; sleep 10; } print "Success: $item\n"; }
Re: Back step in foreach loop
by f00li5h (Chaplain) on Nov 10, 2008 at 23:30 UTC

    Why would the result be different the second time?

    @_=qw; ask f00li5h to appear and remain for a moment of pretend better than a lifetime;;s;;@_[map hex,split'',B204316D8C2A4516DE];;y/05/os/&print;

      Maybe some_test isn't a pure function—say, it tests availability of a certain resource on the network?

      <ot>

      I personally believe I may quote one of my .sig's:

      >> try sleeping on it, that usually works. > I think you're right. Usually it works every time. ;-) I don't know about that. I tried sleeping on a big big problem and we're now divorsed. - "Tralfaz" in sci.math, "Re: About a big big problem" (edited)

      (As code because it's preformatted text.)

      </ot>

      --
      If you can't understand the incipit, then please check the IPB Campaign.
Re: Back step in foreach loop
by rosenrosen (Initiate) on Nov 11, 2008 at 17:32 UTC
    In my case I'm not really worried about an endless loop, but I will implement one of the solutions that checks for it. Thanks for all the feedback, this site looks like a great resource.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://722746]
Approved by Limbic~Region
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-04-19 05:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found