This code 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};
}
__END__
Why it doesn't give this output instead:
$VAR1 = [
'foo',
[
'bar',
'qux'
]
];
Regards,
Gundala Viswanath
Jakarta-INDONESIA
> 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};
This probably isn't doing what you think. Here you try to evaluate the
block consisting on the literal code '$nstr', which of course
evaluates to the value of the variable $nstr.
What you want to is to evaluate the value of $nstr itself. That is
return eval $nstr.
... but in you example $nstr doesn't consist of valid perlcode.
//Makholm
> This code 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?
Consider this, you are attempting to write parser in REs. That won't
get you far. That's like parsing HTML with REs.
I think (if my understanding of description is correct), that
B<Parse::RecDescent> would do the job (I've meant
C<apt-get install libparse-recdescent-perl>). Maybe CPAN has more.
*CUT*
--
Torvalds' goal for Linux is very simple: World Domination