in reply to Perl Array

Is this what you are looking for?

#!/usr/bin/perl # http://perlmonks.org/?node_id=1144405 use Algorithm::Diff qw(traverse_sequences); use strict; use warnings; my $S=" MSATPLTQEQKKA"; my $T=" MNATLTQQTKA"; my @from = split //, shift // $S; my @to = split //, shift // $T; my $top = ''; my $bottom = ''; traverse_sequences( \@from, \@to, { MATCH => sub {print $from[shift()]}, MATCH => sub {$top .= $from[shift()]; $bottom .= $to[pop()]}, DISCARD_A => sub {$top .= $from[shift()]; $bottom .= ' '}, DISCARD_B => sub {$top .= ' '; $bottom .= $to[pop()]}, } ); print "$top\n$bottom\n";

This outputs:

MS ATPLTQEQK KA M NAT LTQ Q TKA

These two sequences are aligned by common character.

Please show inputs and desired output when you ask for help.
Giving several test cases is always a good idea.

Replies are listed 'Best First'.
Re^2: Perl Array
by Anonymous Monk on Oct 11, 2015 at 03:34 UTC

    extra MATCH removed

    #!/usr/bin/perl # http://perlmonks.org/?node_id=1144405 use Algorithm::Diff qw(traverse_sequences); use strict; use warnings; my $S=" MSATPLTQEQKKA"; my $T=" MNATLTQQTKA"; my @from = split //, shift // $S; my @to = split //, shift // $T; my $top = ''; my $bottom = ''; traverse_sequences( \@from, \@to, { MATCH => sub {$top .= $from[shift()]; $bottom .= $to[pop()]}, DISCARD_A => sub {$top .= $from[shift()]; $bottom .= ' '}, DISCARD_B => sub {$top .= ' '; $bottom .= $to[pop()]}, } ); print "$top\n$bottom\n";