in reply to AoA data merging

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; }