Creating a Character Table

Author: L. Grondin

http://rosalind.info/problems/ctbl/

Sample input

(dog,((elephant,mouse),robot),cat);

Sample output

00110
00111

Source code: ctbl-grondilu.pl

use v6;

sub MAIN(Str $input = "(dog,((elephant,mouse),robot),cat);") {
    my $line = $input;
    $line .= chop;
    my $names = rx / <.ident>+ |  >  > /;
    my @name = $line.comb: $names;
    my @sorting = map *.value, sort *.key, (@name Z=> ^@name);
    $line ~~ s:g/$names/0/;
    $line ~~ s:g/','//;

    while $line ~~ / \( 0 ** 2..* \) / {
        my $array = join(
            '',
            .prematch,
            .subst(/0/, '1', :g),
            .postmatch,
        ).subst(/<[()]>/, '', :g) given $/;
        unless $array ~~ /^[ 0+ | 1+ ]$/ {
            say $array.comb[@sorting].join: '';
        }
        $line ~~ s/\( (0+) \)/$0/;
    }
}