Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^3: foreach argument modification inside loop, followed by return from loop

by AnomalousMonk (Monsignor)
on Jul 10, 2013 at 14:49 UTC ( #1043481=note: print w/ replies, xml ) Need Help??


in reply to Re^2: foreach argument modification inside loop, followed by return from loop
in thread foreach argument modification inside loop, followed by return from loop

Do you think it will be more clear if it will return number of element to remove, and we remove it outside for loop?

Yes, IMHO. My approach, using my own conventions, would be something like this (untested):

sub do_finish { my $self = shift; my ($jobid_to_delete) = @_; delete $self->{jobs_h}->{$jobid_to_delete}; my $i_to_delete; my $ar_jobs_a = $self->{jobs_a}; JID: for my $i (0 .. $#$ar_jobs_a) { my $jid = $ar_jobs_a->[$i]{jobid}; next JID unless $jid == $jobid_to_delete; $i_to_delete = $i; last JID; } splice @$ar_jobs_a, $i_to_delete, 1 if defined $i_to_delete; return @$ar_jobs_a ? 'ok' : 'done'; }

Update: Renamed  $jobid to  $jobid_to_delete in interests of self-documentation.


Comment on Re^3: foreach argument modification inside loop, followed by return from loop
Select or Download Code
Re^4: foreach argument modification inside loop, followed by return from loop
by vsespb (Hermit) on Jul 10, 2013 at 14:59 UTC
    Yes, perhaps.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1043481]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (12)
As of 2014-07-11 13:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (224 votes), past polls