Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Back step in foreach loop

by duckyd (Hermit)
on Nov 11, 2008 at 01:43 UTC ( [id://722759] : note . print w/replies, xml ) Need Help??


in reply to Back step in foreach loop

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)

Replies are listed 'Best First'.
Re^2: Back step in foreach loop
by ikegami (Patriarch) on Nov 11, 2008 at 02:00 UTC

    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"; }