I'd just do a standard merge but throw away the results as you go unless the next output is 1+previous.

#!/usr/bin/perl -w use strict; my @list= ( # A more interesting test: [ 101, 105, 204, 312 ], [ 100, 313, 409 ], [ 205, 206, 211, 315 ], [ 106, 207, 210, 314 ], ); # Temporarily get list numbers sorted by first item my @next= sort { $list[$a][0] <=> $list[$b][0] } 0..$#list; # $p is the index of the list to pick from next # $next[$p] says which list to pick from after list $p ( my $p, @next[@next] )= ( @next, -1 ); my @run; # Our consecutive run so far my @from; # Which list we selected each of the above from my @best= ( undef ) x 2; # The run to beat while( 1 ) { # Add smallest remaining item to our run push @run, shift @{ $list[$p] }; push @from, $p; # If we've run out of items or if this one isn't consecutive if( ! defined( $run[-1] ) || 1 < @run && $run[-2]+1 < $run[-1] ) { # then see if the now-finished run is a winner pop @from; if( @best <= @from ) { # We've at least tied, so report if( @best < @from ) { # A new size reached! print "$#run-int runs:$/"; } @best= @run; pop @best; print "$best[0] .. $best[-1] ( @from )$/"; } last if ! defined $run[-1]; # We ran out of items # The next run starts with the item we just added @run= $run[-1]; @from= $p; } elsif( 1 < @run && $run[-1] <= $run[-2] ) { # The lists weren't sorted or our code is broken die "Abort; out of sequence: @run$/"; } # Move $list[$p] now that it has a bigger first item my $n= $next[$p]; if( ! @{ $list[$p] } ) { # Empty. Don't insert $list[$p]. $next[$p]= -2; # Not really needed $p= $n; } elsif( $n < 0 || $list[$p][0] < $list[$n][0] ) { # Do $list[$p] again } else { my $x= $n; my $y= $next[$x]; while( 0 <= $y && $list[$y][0] < $list[$p][0] ) { $y= $next[ $x= $y ]; } # Do list[$p] after $list[$x], before $list[$y]: $next[$x]= $p; $next[$p]= $y; $p= $n; } }

- tye        


In reply to Re: Searching parallel arrays. (just merge) by tye
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.