Problem with Eval in Constructing AoA

  • Thread starter Gundala Viswanath
  • Start date
G

Gundala Viswanath

Hi,

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
 
P

Peter Makholm

Gundala Viswanath said:
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
 
E

Eric Pozharski

Gundala Viswanath said:
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*
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top