in reply to Converting Bracketted String (Newick's) into Array of Array
Sometimes you think "this looks like a perfect task to use these cool RE features," but then you end up with something that does not look as beautiful as planned:
use strict; use warnings; use 5.010; my $str = '((mouse,rat),(human,chimp))'; my $str2 = '(mouse,(human,chimp))'; my @buffers; for ($str, $str2) { @buffers = ([]); m[ ^ ( (?: (\w+) # This serves only to get out fast if we parse # something bad (e.g. where closing parens are # missing) (*PRUNE) (?{ push @{$buffers[-1]}, $2; }) | \( (?{ push @{$buffers[-1]}, my $new = []; push @buffers, $new; }) (?1) (?{ pop @buffers; }) \) ) (?: , (?1) )? ) \z ]x ? do { use Data::Dumper; print Dumper $buffers[0]; } : do { warn "no match: $_\n"; }; }
I look forward to suggestions on how to simplify this code ;)
|
|---|