How to evaluate this string?

Discussion in 'Perl Misc' started by kaushik, Nov 23, 2005.

  1. kaushik

    kaushik Guest

    Hi all,
    I have a string like
    $string='My marks are ( (10+20)*2) '
    i should be able to convert this string to
    $string='My marks are 60 '

    the arithemaic expression may vary to any complexity. it may have
    logical expressions like '&&' or '||' etc.

    Can anyone help me out?

    Thank you
    Kaushik
    kaushik, Nov 23, 2005
    #1
    1. Advertising

  2. kaushik

    Toni Erdmann Guest

    kaushik wrote:
    > Hi all,
    > I have a string like
    > $string='My marks are ( (10+20)*2) '
    > i should be able to convert this string to
    > $string='My marks are 60 '
    >
    > the arithemaic expression may vary to any complexity. it may have
    > logical expressions like '&&' or '||' etc.
    >
    > Can anyone help me out?
    >
    > Thank you
    > Kaushik
    >


    $string =~ s/(\(.*\))/eval($1)/e;

    converts everything that's between first ( and last )
    to the result of it.

    perldoc -f eval

    for more information.

    Toni
    Toni Erdmann, Nov 23, 2005
    #2
    1. Advertising

  3. kaushik

    Matija Papec Guest

    X-Ftn-To: kaushik

    "kaushik" <> wrote:
    >Hi all,
    > I have a string like
    >$string='My marks are ( (10+20)*2) '
    >i should be able to convert this string to
    >$string='My marks are 60 '
    >
    >the arithemaic expression may vary to any complexity. it may have
    >logical expressions like '&&' or '||' etc.


    #untested
    #evaling everything inside ()
    $string =~ s{ ( \(.*\) ) }{

    #you might want to check $1 prior to evaling
    #in case it contains malicious code..
    my $val = eval $1;

    #check perldoc perlvar for $@
    $@ ? "($@)" : $val;
    }xeg;



    --
    Matija
    Matija Papec, Nov 23, 2005
    #3
  4. kaushik wrote:
    > Hi all,
    > I have a string like
    > $string='My marks are ( (10+20)*2) '
    > i should be able to convert this string to
    > $string='My marks are 60 '
    >
    > the arithemaic expression may vary to any complexity. it may have
    > logical expressions like '&&' or '||' etc.
    >
    > Can anyone help me out?


    $string =~ s/(\(.+\))/$1/ee;

    Please read

    perldoc -f eval
    perldoc -q "expand variables"

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Nov 23, 2005
    #4
  5. kaushik wrote:
    > Hi all,
    > I have a string like
    > $string='My marks are ( (10+20)*2) '
    > i should be able to convert this string to
    > $string='My marks are 60 '
    >
    > the arithemaic expression may vary to any complexity. it may have
    > logical expressions like '&&' or '||' etc.
    >
    > Can anyone help me out?


    Can you help us?
    What have you tried so far and where did it not work as expected?

    This is not a machine where you put in a problem and out comes a
    solution. You put in a problem and an attempt for a solution and out
    comes a comment on your solution.

    The solution will depend on how easy it is to detect the expression.
    Once you can do that with a (Perl-) regular expression, the rest would
    be easy.

    Give it a try.
    Hey, you might even learn from trying.
    --
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
    -- T. Pratchett
    Josef Moellers, Nov 23, 2005
    #5
  6. kaushik

    rajesh Guest

    Hi Gunnar Hjalmarsson ,
    I am very very thankful to you.
    your suggestion is reallly superb. even though it is quite simple ,
    very difficult to get the idea. Again thanks a lot. I will ask if any
    further queries in future and hope you continue the help.

    Regards,
    Rajesh
    Kaushik

    Gunnar Hjalmarsson wrote:
    > kaushik wrote:
    > > Hi all,
    > > I have a string like
    > > $string='My marks are ( (10+20)*2) '
    > > i should be able to convert this string to
    > > $string='My marks are 60 '
    > >
    > > the arithemaic expression may vary to any complexity. it may have
    > > logical expressions like '&&' or '||' etc.
    > >
    > > Can anyone help me out?

    >
    > $string =~ s/(\(.+\))/$1/ee;
    >
    > Please read
    >
    > perldoc -f eval
    > perldoc -q "expand variables"
    >
    > --
    > Gunnar Hjalmarsson
    > Email: http://www.gunnar.cc/cgi-bin/contact.pl
    rajesh, Nov 24, 2005
    #6
  7. rajesh wrote:
    > Gunnar Hjalmarsson wrote:
    >> kaushik wrote:
    >>> I have a string like
    >>> $string='My marks are ( (10+20)*2) '
    >>> i should be able to convert this string to
    >>> $string='My marks are 60 '
    >>>
    >>> the arithemaic expression may vary to any complexity. it may have
    >>> logical expressions like '&&' or '||' etc.
    >>>
    >>> Can anyone help me out?

    >>
    >> $string =~ s/(\(.+\))/$1/ee;
    >>
    >> Please read
    >>
    >> perldoc -f eval
    >> perldoc -q "expand variables"

    >
    > I am very very thankful to you. your suggestion is reallly superb.
    > even though it is quite simple , very difficult to get the idea.


    I'm glad you are thankful, but if I were you, I'd also take a look at
    Matija's suggestion, which displays possible errors.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Nov 24, 2005
    #7
  8. kaushik

    rajesh Guest

    Thanks Matija,

    Regards
    Rajesh
    rajesh, Nov 24, 2005
    #8
  9. kaushik

    rajesh Guest

    Hi Hjalmarsson,
    I have checked it .The error coming out is the
    $val requires explicit package name. shall we add any other directive
    if we use eval as matija used?

    Thanks
    Rajesh
    rajesh, Nov 24, 2005
    #9
  10. [ Please provide some context when replying to a message. Most people,
    who may be able to help, do _not_ access this Usenet group via Google. ]

    rajesh wrote:
    > I have checked it .


    What have you checked? Oh, are you talking about Matija's code suggestion?

    > The error coming out is the $val requires explicit package name.


    Matija's code works just fine for me. If it doesn't for you, please post
    a short but complete program, that people can copy and run, and that
    demonstrates the problem you are experiencing.

    Just as is recommended in the posting guidelines for this group, btw:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Nov 24, 2005
    #10
    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. Brad Smith
    Replies:
    2
    Views:
    474
    Blaise Pascal Tine
    Jun 27, 2003
  2. TJS

    Evaluate a string

    TJS, May 22, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    2,878
  3. Kamal Ahmed
    Replies:
    2
    Views:
    6,004
    Sampathd
    Oct 21, 2005
  4. Son KwonNam
    Replies:
    4
    Views:
    6,992
    Joris Gillis
    Jan 5, 2005
  5. Marco Traverso
    Replies:
    7
    Views:
    1,720
    Marco Traverso
    Dec 6, 2003
Loading...

Share This Page