Here's make take on the problem. I haven't looked at the other solutions, so it will be interesting to see how the others compare.

Since the numbers are contiguous unique across all arrays, a simple hash will do the trick, whose key is the orginal number in the source array, and the value is a reference to a list that stores an identifier that stands in place of the array, and the offset within the array.

I then take the sequence numbers from an array, and look for sequences of four. When I find one, I see how far it can be extended whilst keeping the sequence contiguous.

#! /usr/local/bin/perl -w use strict; use constant CONTIG => 4; my @a1 = ( 100, 204, 312 ); my @a2 = ( 102, 313, 409 ); my @a3 = ( 205, 206, 315 ); my @a4 = ( 207, 210, 314 ); my %seen; my $off = 0; $seen{$_} = [1, $off++] for @a1; $off = 0; $seen{$_} = [2, $off++] for @a2; $off = 0; $seen{$_} = [3, $off++] for @a3; $off = 0; $seen{$_} = [4, $off++] for @a4; my @seq = sort {$a <=> $b} keys %seen; my $idx = 0; while ($idx <= $#seq - (CONTIG-1) ) { if ($seq[$idx] + CONTIG-1 < $seq[$idx + CONTIG-1]) { ++$idx; } else { # at least 4 contiguous (CONTIG) sequence numbers # see how far we can extend the range my $range = CONTIG-1; while ($idx+$range+1 <= $#seq and $seq[$idx]+$range+1 == $seq[$idx+$range+1] ) { ++$range; } print "saw $seq[$idx]..$seq[$idx+$range]\n\t", join( "\n\t", map {pretty($seen{$_})} @seq[$idx .. $idx+$range] ), "\n"; $idx += $range; } } sub pretty { my $r = shift; return "array $r->[0], offset $r->[1]"; }

(update: erm, and here's the output:)

saw 204..207 array 1, offset 1 array 3, offset 0 array 3, offset 1 array 4, offset 0 saw 312..315 array 1, offset 2 array 2, offset 1 array 4, offset 2 array 3, offset 2

• another intruder with the mooring in the heart of the Perl


In reply to Re: Searching parallel arrays. by grinder
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.