in reply to Comparing and Aligning arrays
G'day abd,
Welcome to the monastery.
As others have pointed out, your requirements are unclear and, as you've shown all data as paragraph text, the output format cannot be determined.
Also note that "It's very important for me to keep the original order." does not seem to match your required output: 1st input row is a a b and 1st output row is a a - (only the first two characters are in the same order); 1st input column is a b c and 1st output column is a b c - (only the first three characters are in the same order). You need to clarify what "original order" you are referring to.
Assuming both instances of "List1 List2 List3" are part of the description, i.e. not part of input or output data, then the following code transforms your provided input to your wanted output.
#!/usr/bin/env perl use strict; use warnings; my %list = (1 => [qw{a b c}], 2 => [qw{a b d}], 3 => [qw{b c d}]); my (%align, %seen); %{$align{$_}} = map { ++$seen{$_}; $_ => undef } @{$list{$_}} for keys + %list; for my $char (sort keys %seen) { for my $list (sort keys %align) { print exists $align{$list}{$char} ? $char : '-', ' '; } print "\n"; }
Output:
a a - b b b c - c - d d
[Please read the guidelines in "How do I post a question effectively?" and follow them in any future postings.]
-- Ken
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Comparing and Aligning arrays
by AnomalousMonk (Archbishop) on Apr 04, 2014 at 19:55 UTC | |
by kcott (Archbishop) on Apr 04, 2014 at 22:45 UTC | |
|
Re^2: Comparing and Aligning arrays
by abd (Initiate) on Apr 04, 2014 at 12:58 UTC |