Problem with Eval in Constructing AoA

Discussion in 'Perl Misc' started by Gundala Viswanath, Sep 4, 2008.

  1. 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
     
    Gundala Viswanath, Sep 4, 2008
    #1
    1. Advertising

  2. Gundala Viswanath <> 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
     
    Peter Makholm, Sep 4, 2008
    #2
    1. Advertising

  3. Gundala Viswanath <> 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
     
    Eric Pozharski, Sep 4, 2008
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eric Newton
    Replies:
    3
    Views:
    9,422
    Brock Allen
    Apr 4, 2005
  2. DataBinder.Eval and Eval.

    , Jun 16, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    550
    Karl Seguin [MVP]
    Jun 16, 2006
  3. Alex van der Spek

    eval('07') works, eval('08') fails, why?

    Alex van der Spek, Jan 8, 2009, in forum: Python
    Replies:
    6
    Views:
    1,461
    Bruno Desthuilliers
    Jan 8, 2009
  4. Chris McMahon

    construct AoA with REXML XPath?

    Chris McMahon, Mar 15, 2006, in forum: Ruby
    Replies:
    3
    Views:
    117
    William James
    Mar 16, 2006
  5. Vishal G

    Merging/Joining AoA

    Vishal G, Apr 22, 2008, in forum: Perl Misc
    Replies:
    12
    Views:
    150
    Ben Bullock
    Apr 24, 2008
Loading...

Share This Page