BrowserUk,
I haven't had a chance to optimize this or even to test it thoroughly but I wanted to share. It is commented but if you need further explanation, please don't hesitate.
#!/usr/bin/perl use strict; use warnings; my @aoa = ([100, 204, 312], [102, 313, 409], [205, 206, 315], [207, 21 +0, 314]); my $next = gen_find_contiguous(4, \@aoa); while (my @seq = $next->()) { print "Val: $_->[0]\tArr: $_->[1]\tPos: $_->[2]\n" for @seq; print "\n"; } sub gen_find_contiguous { my ($min, $list) = @_; my @pos = (0) x @$list; return sub { my $found; my @cont; while (! $found) { # Find smallest value keeping track of array and index in +that array my ($arr, $val, $idx); for (grep defined($pos[$_]), 0 .. $#pos) { if (! defined $arr || $list->[$_][$pos[$_]] < $val) { ($arr, $val, $idx) = ($_, $list->[$_][$pos[$_]], $ +pos[$_]); } } # Check to see if we are at the end of all arrays if (! defined $arr) { return @cont >= $min ? @cont : (); } # Add value to cont if empty if (! @cont) { push @cont, [$val, $arr, $idx]; } else { # Add value to cont if it 1 away from prev value if ($val - $cont[-1][0] == 1) { push @cont, [$val, $arr, $idx]; } else { # If @cont >= $min, set found flag if (@cont >= $min) { $found = 1; } # Else start over else { @cont = [$val, $arr, $idx]; } } } # Increment index of array we fetched value from if ! $fou +nd if (! $found) { $pos[$arr] = $idx == $#{$list->[$arr]} ? undef : $idx + 1; } } return @cont; }; }

I didn't use List::Util's reduce() because apparently it can handle multiple return values. The docs (Returns the result of the last call to BLOCK.) should probably be clarified on this point

Cheers - L~R


In reply to Re^3: Searching parallel arrays. by Limbic~Region
in thread Searching parallel arrays. by BrowserUk

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.