Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Comparing and Aligning arrays

by AnomalousMonk (Monsignor)
on Apr 04, 2014 at 02:15 UTC ( #1081057=note: print w/ replies, xml ) Need Help??


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", "-"], ]


Comment on Re: Comparing and Aligning arrays
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1081057]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2014-08-23 02:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (171 votes), past polls