in reply to Comparing and Aligning arrays
I, also, don't understand the rule for positioning the '-' (hyphen) filler character(s), but here's an approach. (Update: I guess I should also admit I don't really understand 'alignment' either as used in the context of the OP.) Maybe not the most efficient, but I'm hopelessly addicted to regexen.
c:\@Work\Perl\monks>perl -wMstrict -le "use List::Util qw(max); use Data::Dump; ;; my @List1 = qw(a a b); my @List2 = qw(b b c); my @List3 = qw(c d d); ;; my @runs = do { my $cat = join '', @List1, @List2, @List3; my $s; grep $s = !$s, $cat =~ m{ ((.) \2*) }xmsg; }; ;; my $longest = -1 + max map length, @runs; my @padding = ('-') x $longest; ;; my @padded = map [ (split(''), @padding)[0 .. $longest] ], @runs; dd \@padded; " [ ["a", "a", "-"], ["b", "b", "b"], ["c", "c", "-"], ["d", "d", "-"], ]
Update: Here's another, slightly different approach using unpack; might be slightly faster.
c:\@Work\Perl\monks>perl -wMstrict -le "use List::Util qw(max); use Data::Dump; ;; my @List1 = qw(a a b); my @List2 = qw(b b c); my @List3 = qw(c d d); ;; my @runs = do { my $cat = join '', @List1, @List2, @List3; my $s; grep $s = !$s, $cat =~ m{ ((.) \2*) }xmsg; }; ;; my $max_pad = -1 + max map length, @runs; my $padding = '-' x $max_pad; ;; my @padded = map [ unpack qq{a (a)$max_pad}, $_ . $padding ], @runs; dd \@padded; " [ ["a", "a", "-"], ["b", "b", "b"], ["c", "c", "-"], ["d", "d", "-"], ]
|
|---|