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?

$_="msh210";$"=$\;@_=@{[split//,uc]}[2,0];$_="@_$\1";$\=$/;++$_[0]for$...1;print lc substr crypt($_,"@_"),1,6

In reply to Switching back and forth between parts of my script by msh210

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.