Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
To do this, I need to reconcile the discrepancies between the lists and come up with an array of arrays holding the indexes.
Have a look at List::Compare. Following code does build a hash of arrays holding the indexes (you may need to replace the ' ' string (being used for undef values) with undef; I used it in favor of undef, because undef would garble the output). Basically, it should work for an infinite amount of sets.
#!/usr/bin/perl use strict; use warnings; use List::Compare; my (%have, @seen, %is_unique); 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 $lcm = List::Compare->new(@sets); foreach my $set (@sets) { my %seen = map { $_ => 1 } @$set; push @seen, \%seen; } foreach ($lcm->get_intersection) { $have{$_} = [ 0..$#sets ]; } my %index = map { $_ => undef } 0..$#sets; foreach my $i (0..$#sets) { foreach my $elem ($lcm->get_unique($i)) { my @list; foreach (keys %index) { $list[$_] = $index{$_} } $list[$i] = $i; $have{$elem} = [ @list ]; $is_unique{$elem}++; } } foreach my $elem ($lcm->get_nonintersection) { next if $is_unique{$elem}; my @list; foreach (keys %index) { $list[$_] = $index{$_} } foreach my $i (0..$#sets) { $list[$i] = $i if $seen[$i]{$elem} } $have{$elem} = [ @list ]; } foreach (sort keys %have) { foreach my $i (0..@{$have{$_}}) { print defined $have{$_}->[$i] ? "$_ " : ' '; } print "\n"; } __OUTPUT__ a a a b c c c d d e e e f f f g g g h h h i i i

In reply to Re: Reconciling multiple lists (similar to "merge" in CVS?) by shigetsu
in thread Reconciling multiple lists (similar to "merge" in CVS?) by japhy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2024-04-16 04:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found