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

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.