Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Problem with Eval in Constructing AoA

Reply
Thread Tools

Problem with Eval in Constructing AoA

 
 
Gundala Viswanath
Guest
Posts: n/a
 
      09-04-2008
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:umper;
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
 
Reply With Quote
 
 
 
 
Peter Makholm
Guest
Posts: n/a
 
      09-04-2008
Gundala Viswanath <(E-Mail Removed)> writes:

> 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
 
Reply With Quote
 
 
 
 
Eric Pozharski
Guest
Posts: n/a
 
      09-04-2008
Gundala Viswanath <(E-Mail Removed)> wrote:
> 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?


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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
eval('07') works, eval('08') fails, why? Alex van der Spek Python 6 01-08-2009 08:24 PM
Merging/Joining AoA Vishal G Perl Misc 12 04-24-2008 03:16 AM
DataBinder.Eval and Eval. craigkenisston@hotmail.com ASP .Net 1 06-16-2006 05:33 PM
construct AoA with REXML XPath? Chris McMahon Ruby 3 03-16-2006 04:50 AM
DataBinder.Eval for an object's property property... like Eval(Container.DataItem,"Version.Major") Eric Newton ASP .Net 3 04-04-2005 10:11 PM



Advertisments