And some (mediocre :-) code:
#!/usr/bin/perl -w use strict; my @source_data = ( [ 100, 204, 312 ], [ 102, 313, 409 ], [ 205, 206, 315 ], [ 207, 210, 314 ], ); my @sorted_numbers = sort map { @{$_} } @source_data; # Using hash for the lookup. If your N numbers are fairly dense over # 0..N then use an array for reduced mem consumption my %from_whence; foreach my $source_array (@source_data) { my $index = 0; foreach my $number (@$source_array) { $from_whence{$number} = [$source_array, $index]; ++$index; } }; # Look for 4 contig values my @recent; foreach my $number (@sorted_numbers) { push @recent, [ $number, $from_whence{$number} ]; next unless scalar @recent > 4; shift @recent; check_and_handle_contig(\@recent); } exit 0; sub check_and_handle_contig { my $numbers_info = shift; my $lastnum = undef; foreach my $numinfo (@$numbers_info) { my $num = $numinfo->[0]; if ($lastnum) { return unless $num == $lastnum + 1; } $lastnum = $num; } print "Found sequence:\n"; foreach my $numinfo (@$numbers_info) { my $num = $numinfo->[0]; my $info = $numinfo->[1]; print "$num from $info->[0]:$info->[1]\n"; } }
which prints:
Found sequence: 204 from ARRAY(0x814bc28):1 205 from ARRAY(0x8188e94):0 206 from ARRAY(0x8188e94):1 207 from ARRAY(0x8188ed0):0 Found sequence: 312 from ARRAY(0x814bc28):2 313 from ARRAY(0x8188c30):1 314 from ARRAY(0x8188ed0):2 315 from ARRAY(0x8188e94):2
Update: Limbic~Region++ noted that the sort in the above (more mediocre than I thought :-) code should be numeric, rather than the default sort. Update 2:And there's another bug, found by Not_a_Number, fixes below.

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