Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

parsing a function

by hideki-san (Initiate)
on May 03, 2003 at 06:54 UTC ( [id://255267]=perlquestion: print w/replies, xml ) Need Help??

hideki-san has asked for the wisdom of the Perl Monks concerning the following question:

I have an function t^n*f(t). Lets say that n is a number: t^234.57*f(t) This function t^n*f(t) can be a part of a larger function. Example: b+c(t)+t^234.57*f(t)+Z. "n" can be any number, and does not necessarily have a decimal point every time. How do I parse out n to a var named $nvar? Basically I want to recognize the t^n*f(t) function first, and then get n. I made an attempt at the regex, and gave up.

Replies are listed 'Best First'.
Re: parsing a function
by jgallagher (Pilgrim) on May 03, 2003 at 07:19 UTC
    How about:
    #!/usr/bin/perl -w use strict; my @func = ( "b+c(t)+t^234.57*f(t)+Z", "a + b + c + t^324*f(t) + t^f(t)", "t^123456.3990*f(t) + 4", "12 + bt + t^32*f(t)", "a+b+c(t)+t^.23*f(t)+Z" ); foreach (@func) { if (/t\^(\d+\.?\d*|\.\d+)\*f\(t\)/) { print "$_\n n = $1\n\n"; } }
    Output:
    b+c(t)+t^234.57*f(t)+Z n = 234.57 a + b + c + t^324*f(t) + t^f(t) n = 324 t^123456.3990*f(t) + 4 n = 123456.3990 12 + bt + t^32*f(t) n = 32 a+b+c(t)+t^.23*f(t)+Z n = .23
    Note that this will only match one t^n*f(t) per function.
Re: parsing a function
by bart (Canon) on May 03, 2003 at 23:09 UTC
    I think what you really want, is an expression parser. Regexes can't handle this kind of stuff, especially if you start using recursive definitions, with subexpressions between parens... You might want to check out Parse::RecDescent, which was created with this kind of application in mind. Start by the tutorial, included with the package on CPAN. n.b. You'll also need to install the module Text::Balanced, which used to be part of this distribution.

    In addition, I found an example script for a calculator on the web, here. Despite it requiring a perl 5.8.0, it appears to work well with perl 5.6.1, if you comment out the "use 5.008;" line. Don't forget to call the sub shell.

Re: parsing a function
by vladb (Vicar) on May 03, 2003 at 07:07 UTC
    How do I parse out n to a var named $nvar?

    I'm either confused or failing to understand your question, but is a simple search and replace what you want to do here? In which case, you'd be looking at this code:
    my $function = 'b+c(t)+t^234.57*t^n*f(t)+Z'; $function =~ s/n/\$nvar/g; print "New function: $function\n";
    Which should print

    New function: b+c(t)+t^234.57*t^$nvar*f(t)+Z

    In case I got you wrong, however, could you please clarify your question? ;-)

    _____________________
    # Under Construction

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://255267]
Approved by Paladin
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-03-28 22:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found