Based on the suggestions offered here and some discussions with other programmers, this is the working code I came up with, for anyone who is interested. Thanks for all the great suggestions!

sub merge { my @allparts = @_; # make an array of just the end points my @ends = map{ [$_->[0], $_->[-1]] } @allparts; # give us a starting point my @sorted = (shift(@ends)); # while we still have unsorted ends OUTER: while(@ends) { # check each of the unsorted ends to see if they can # attach, we only compare them to the last element of # @sorted because if we have more than one element in # @sorted, then we already checked all of @ends against # the elements at the beginning for my $x (0 .. $#ends) { # see if we can attach it at the beginning if($sorted[-1]->[0] == $ends[$x]->[1]) { unshift(@{$sorted[-1]},@{splice(@ends,$x,1)}); next OUTER; } # see if we can attach it at the end if($ends[$x]->[0] == $sorted[-1]->[-1]) { push(@{$sorted[-1]},@{splice(@ends,$x,1)}); next OUTER; } } # if we get here, then we didn't find # any @ends that attach to anything # in @sorted, so we grab one of the # @ends to make a new section, and # continue push(@sorted,shift(@ends)); } # now go through the sections in @sorted, and replace # them with the appropriate complete data from @allparts. for my $x (0 .. $#sorted) { my @ends = @{$sorted[$x]}; my @full = (); # for each start/end combination while(my($start,$end) = splice(@ends,0,2)) { # find the chunk it came from #print "start=$start end=$end\n"; foreach(@allparts) { #print "0=$_->[0]\n"; #print Dumper($_); if(($_->[0] == $start) && ($_->[-1] == $end)) { pop(@full); # get rid of the duplicate piece push(@full,@{$_}); last; } } } $sorted[$x] = [@full]; } return @sorted; }

In reply to Re: AoA data merging (final answer) by jasonk
in thread AoA data merging by jasonk

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.