Ack! I overlooked the unsorted elements in your data. By way of apology, please accept this working, tested code. It is somewhat under-documented
and needs refactoring, but I am out of time tonight. For your two sample @sets, my code outputs:
[ 0, undef, 1, undef, undef, 2, 3, 4, 5, 6 ],
[ 0, 1, 2, 3, 4, 5, 6, undef, 7, 8 ],
[ 0, undef, 1, 2, 3, 4, 5, undef, 6, 7 ],
and:
[ 0, 1, 2, undef, undef, 3, 4 ],
[ 0, 1, undef, 2, undef, 3, 4 ],
[ 0, 1, undef, undef, 2, 3, 4 ],
.
The code works by transforming all the lists into AoAs, with each element holding [$original_item, $original_index].
It merges the first two lists, via Algorithm::Diff (++belg4mit) to form elements [$original_item, $index_from_list1, $index_from_list2]. List 3 is then merged in, etc.
I *think* it will work fine for *any* number of lists.
#!/usr/bin/perl
use strict;
use warnings;
use Algorithm::Diff;
#use Data::Dumper; $Data::Dumper::Useqq = 1; $| = 1;
{
my @sets = (
[ qw( a c f g e h i ) ], # X
[ qw( a b c d e f g h i ) ], # Y
[ qw( a c d e f g h i ) ], # Z
);
# my @sets = (
# [ qw( a b c f g ) ], # X
# [ qw( a b d f g ) ], # Y
# [ qw( a b e f g ) ], # Z
# );
my @all_merged = merge_multiple_lists( @sets );
print dump_rotated_indexes_of_lists( @all_merged );
# foreach (@all_merged) {
# my ( $item, @offsets ) = @{ $_ };
# @offsets = map { defined $_ ? $_ : ' ' } @offsets;
# print "$item @offsets\n";
# }
exit;
}
# Rotate @all_merged 90 degrees to match the expected output.
sub dump_rotated_indexes_of_lists {
my @lists = @_;
my @idx;
foreach (@lists) {
my ( $item, @offsets ) = @{ $_ };
foreach my $i ( 0 .. $#offsets ) {
push @{ $idx[$i] }, $offsets[$i];
}
}
foreach (@idx) {
my $dump = join ', ',
# map { sprintf '%5s', $_ }
map { defined $_ ? $_ : 'undef' }
@{ $_ };
print " [ $dump ],\n";
}
}
sub merge_multiple_lists {
my @list_arefs = @_;
foreach my $list_aref ( @list_arefs) {
my $count = 0;
$list_aref = [ map { [ $_, $count++ ] } @{ $list_aref } ];
}
my $accumulator_aref = shift @list_arefs;
my $offset_width = 1;
foreach my $next_to_merge_aref ( @list_arefs ) {
$accumulator_aref = merge_two_lists(
$accumulator_aref, $next_to_merge_aref, $offset_width
);
++$offset_width;
}
return @{ $accumulator_aref };
}
# Each list element will be an aref.
# The item to be compared is in position 0.
sub key_on_first_element {
my ($self) = @_;
return $self->[0];
}
sub merge_two_lists {
my ( $list1_aref, $list2_aref, $offset_width ) = @_;
$offset_width = 1 if not defined $offset_width;
my $diff = Algorithm::Diff->new(
$list1_aref,
$list2_aref,
{ keyGen => \&key_on_first_element },
);
my @merged;
while( $diff->Next() ) {
my @indices1 = $diff->Range(1);
my @indices2 = $diff->Range(2);
my $which_list = $diff->Diff();
# 0 means lists are identical in this chunk.
if ( $which_list == 0 ) {
die "Can't happen" if @indices1 != @indices2;
foreach my $i ( 0 .. $#indices1 ) {
my ( $item1, @offsets ) = @{ $list1_aref->[ $indices1[
+$i] ] };
my ( $item2, $offset ) = @{ $list2_aref->[ $indices2[
+$i] ] };
die unless $item1 eq $item2;
push @offsets, $offset;
push @merged, [ $item1, @offsets ];
}
}
# 1 means that only list 1 has entries in this chunk
elsif ( $which_list == 1 ) {
foreach ( @{ $list1_aref }[ @indices1 ] ) {
my ( $item1, @offsets ) = @{ $_ };
push @offsets, undef;
push @merged, [ $item1, @offsets ];
}
}
# 2 means that only list 2 has entries in this chunk
elsif ( $which_list == 2 ) {
foreach ( @{ $list2_aref }[ @indices2 ] ) {
my ( $item2, $offset ) = @{ $_ };
my @offsets = map { undef() } 1 .. $offset_width;
push @offsets, $offset;
push @merged, [ $item2, @offsets ];
}
}
# 3 means both lists have entries in this chunk.
# XXX
# This is the "each set having a unique element" situation.
# Extra code may be needed to put these into a desired order.
elsif ( $which_list == 3 ) {
# XXX Dup code from which_list==(1|2)
foreach ( @{ $list1_aref }[ @indices1 ] ) {
my ( $item1, @offsets ) = @{ $_ };
push @offsets, undef;
push @merged, [ $item1, @offsets ];
}
foreach ( @{ $list2_aref }[ @indices2 ] ) {
my ( $item2, $offset ) = @{ $_ };
my @offsets = map { undef() } 1 .. $offset_width;
push @offsets, $offset;
push @merged, [ $item2, @offsets ];
}
}
else {
die "Can't happen";
}
}
return \@merged;
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.