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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |