in reply to Parsing newick trees

This is really quick and dirty. Basically, if we can assume that the format is very strict (values don't have any quoting, and commas and parentheses are always in sane places), you can just do some replacements on the input and then eval out a result.

use Data::Dumper; my $tree_in = '(A,(B,C))'; #my $tree_in = "((A,(B,((C,(D,E)),(F,G)))),H)"; $tree_in =~ s/([^,()]+)/\{VALUE=>"$1"\}/g; $tree_in =~ s/\(/\{LEFT=>/g; $tree_in =~ s/,/,RIGHT=>/g; $tree_in =~ s/\)/\}/g; print $tree_in, "\n"; my $tref = eval $tree_in; print Dumper $tref; __END__ {LEFT=>{VALUE=>"A"},RIGHT=>{LEFT=>{VALUE=>"B"},RIGHT=>{VALUE=>"C"}}} $VAR1 = { 'LEFT' => { 'VALUE' => 'A' }, 'RIGHT' => { 'LEFT' => { 'VALUE' => 'B' }, 'RIGHT' => { 'VALUE' => 'C' } } };

As I say, it's quick and dirty, there's no input validation, and the door's wide open to hostile inputs (think, "run arbitrary Perl code"). It wouldn't be too hard to put some rules on the front end to make some of these problems go away, but in the end you may be happier with a more complete parsing solution.