Gundala Viswanath wrote:
Hi,
Hello,
This cowde below tries to convert the string in newick format into the corresponding data structure (Array of Array). But somehow the EVAL function doesn't work as expected. What's wrong with my code here? __BEGIN__ use Data::Dumper; use Carp; my $str = "(foo,(bar,qux))"; #Newick format print "$str\n"; my $ar = conv_newick2aoa($str); print Dumper $ar ; sub conv_newick2aoa { my $nstr_in = shift; my $nstr = $nstr_in; for ($nstr) { s/\\/\\\\/g; s/'/\\'/g; s/\(/['/g; s/\)/']/g; s/,/','/g; } return eval{$nstr};
Change that to: for ( $nstr ) { s/\\/\\\\/g; s/'/\\'/g; s/([^,()]+)/'$1'/g; tr/()/[]/; } return eval $nstr;
} __END__ Why it doesn't give this output instead: $VAR1 = [ 'foo', [ 'bar', 'qux' ] ];
Because you are creating the string "['foo','['bar','qux']']" which doesn't translate into an array of arrays and you are using block eval instead of string eval.
John -- Perl isn't a toolbox, but a small machine shop where you can special-order certain sorts of tools at low cost and in short order. -- Larry Wall -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/