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
In reply to Re: Comparing and Aligning arrays
by kcott
in thread Comparing and Aligning arrays
by abd
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |