Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

comment on

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

I see that, however from what I can see that whole file could be easily refactored to not only avoid deleting within the (outer) for loop, but be significantly more concise as well. Here's a grossly oversimplified version of your code logic (at least the parts pertinent to this discussion):

OUTER: for (1) { for my $job (@jobs) { my ($status, $task) = $job->{job}->get_task(); if ($status eq 'wait') { if ($self->{one_by_one}) { return 'wait'; } else { return 'wait' unless --$maxcnt; } } elsif ($status eq 'done') { ($s, $t) = do_finish($job); # do_finish deletes from @jobs redo OUTER if $s eq 'ok'; return ($s,$t); # (else) } else { my $newtask = Package->new(...); return ($status, $newtask); } } } return('wait')

You might already get away with the do_finish call, since you either redo outside the for, or exit the sub, but it might not stay that way if your logic changes in the future (it does create a bit of a trap for the unwary).

As far as I can tell, you only ever look at the first element of the jobs array; all cases eventually lead to a return or redo OUTER, right? If that's the case, you don't need to loop through the jobs at all. And, one more optimization: by extension of the last point, you only ever call do_finish on that first job (in the get_task loop context), right? So you don't need splice at all, just shift.

NEXT: my $job = shift @jobs; my ($status, $task) = $job->{job}->get_task; return 'wait' if $status eq 'wait'; # All 'wait' roads went there. if ($status eq 'done') { my ($done_s, $done_t) = $self->do_finish($job); goto NEXT if $done_s eq 'ok'; return ($done_s, $done_t); } my $newtask = Package->new(...); return ($status, $newtask);

I haven't had much time with your code at all, so it's quite possible I missed something (or there are planned additions that would invalidate my approach). Just remember my main goal was to get you thinking about one way you might start to rework the outer code for easier human comprehension, conciseness, not to mention getting rid of any need to flirt with array modification inside a loop.

And this is just one of many ways. Let me/us know which way you end up going, or if you'd like more input.


In reply to Re^5: foreach argument modification inside loop, followed by return from loop by rjt
in thread foreach argument modification inside loop, followed by return from loop by vsespb

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 about the Monastery: (9)
As of 2024-04-23 11:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found