msh210 has asked for the wisdom of the Perl Monks concerning the following question:
O Monks,
I have a script that looks essentially like this:
while(@array1) { ONE: while(@array1) { my $item = shift @array1; # Do various things with $item, some of which will _possibly_ # push @array1, ... # and/or # push @array2, ... } TWO: while(@array2) { my $item = shift @array2; # Do various things with $item, some of which will _possibly_ # push @array2, ... # and/or # push @array1, ... } }
The idea here is that I'm acting on items, which includes collecting more items to act on. The problem is that ONE runs for a long, long time (a few days, so far), and I don't want to spend so much time on @array1 and neglect @array2: I want to get some of the "various things" done to @array2 also. So I'm looking for ideas on how to get my code to switch off to TWO even before ONE ends.
One thing I thought of is to use an incrementer, something like this:
while(@array1 or @array2) { my $i = 0; ONE: while(@array1 and $i < $some_large_number/10) { $i++; my $item = shift @array1; # Do various things (as above) } $i = 0; TWO: while(@array2 and $i < $some_large_number) { $i++; my $item = shift @array2; # Do various things (as above) } }
Another idea I had was to use threading: handle ONE and TWO in separate threads, and have each wait for the other until both arrays are empty. The problem with using threading is twofold: I don't know how to use threading (though I can learn, of course), and it comes with a big nasty warning.
A third idea I had was to combine the two arrays, something like this:
while(@array) { my $item = shift @array; if (used_to_be_in_array1($item)) { # Do various array1ish things with $item, some of which will _ +possibly_: push @array, ... } elsif (used_to_be_in_array2($item)) { # Do various array2ish things with $item, some of which will _ +possibly_: push @array, ... } else { warn $item; } }
But I may be missing some pros and cons of each of those approaches — and of course there is probably yet another and better way that I haven't thought of. So I seek your advice on this issue. What do you recommend (and why) for handling both my arrays instead of spending a lot of time on one of them?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Switching back and forth between parts of my script
by Laurent_R (Canon) on Mar 04, 2016 at 18:06 UTC | |
|
Re: Switching back and forth between parts of my script
by Tanktalus (Canon) on Mar 04, 2016 at 19:48 UTC | |
|
Re: Switching back and forth between parts of my script
by jdporter (Paladin) on Mar 04, 2016 at 21:58 UTC | |
by msh210 (Monk) on Mar 14, 2016 at 18:32 UTC | |
|
Re: Switching back and forth between parts of my script
by perlfan (Parson) on Mar 04, 2016 at 18:39 UTC | |
|
Re: Switching back and forth between parts of my script
by Laurent_R (Canon) on Mar 05, 2016 at 08:56 UTC | |
by msh210 (Monk) on Mar 14, 2016 at 18:33 UTC | |
|
Re: Switching back and forth between parts of my script
by msh210 (Monk) on Mar 04, 2016 at 21:08 UTC | |
by perlfan (Parson) on Mar 07, 2016 at 21:51 UTC |