in reply to Wait and retry loop without goto

Untested
# refactor each $router to have a 'success' key of 0; # running this way allows you to iterate through the # entire list of routers, dealing with failures only # after the entire stack has been processed. # assuming a sufficient number of targets, this should # sidestep your problem as I understand it. for (my $i = 0; $i++; $i < 3) { my $failures = 0; foreach my $router (@routers) { if ($router->{'success'} != 1) { my $output = `expect -f show_running_config.exp $router->[$IP]`; if (write_config($output, "$router->[$NAME].cfg") == 0) { $router->{'success'} = 1; $router->{'msg'} = "Output was $output"; } else { $failures++; $router->{'msg'} = "Failed to get config for $router->[$NAME] +at $router->[$IP]: $output\n"; if ($i == 2) { print $router->{'msg'}; } } } } if ($failures < 1) { last; } sleep 5; $i++; }
-- Edit: removed unneeded $failures. -- Edit2: Thanks Laurent_R for proofreading.