Funky val() operation.
Author Message
Funky val() operation.

I'm trying to take user input to create a mathematical function of a
variable (ex: "X^2" "-X + 2" run of the mill f(x)'s) by taking input
from a text box as a string, searching for the variable and replacing it
with the correct value.  I need it to return a numeric value though.  So
I thought val() would work well.  It doesn't though.

For example,
val(1 + 2) ' would return 3, val(4^2 + 5) would return 21, and so on

But...
n = "1 + 2"
val(n) ' returns "1"
n = "4^2 + 5"
val(n) ' returns "4"

It seems to return the first number in the string.  Does anyone know how
to remedy this?  Is there a better way that I could go about
accomplishing my goal?

Thanks

Fri, 23 Feb 2001 03:00:00 GMT
Funky val() operation.

Quote:

>I'm trying to take user input to create a mathematical function of a
>variable (ex: "X^2" "-X + 2" run of the mill f(x)'s) by taking input
>from a text box as a string, searching for the variable and replacing
it
>with the correct value.  I need it to return a numeric value though.
So
>I thought val() would work well.  It doesn't though.

>For example,
>val(1 + 2) ' would return 3, val(4^2 + 5) would return 21, and so on

>But...
>n = "1 + 2"
>val(n) ' returns "1"
>n = "4^2 + 5"
>val(n) ' returns "4"

>It seems to return the first number in the string.  Does anyone know
how
>to remedy this?  Is there a better way that I could go about
>accomplishing my goal?

You are asking for a "recursive descent parser". These have been written
for VB; some small enough to fit in a VB Class, but there's another way.

VB always has a database close at hand. Open a connection to it, and
execute a line like this:

rs = db.OpenRecordset ("select 4^2 + 5")

With a little fudging you should be able to beat 21 out of
'rs.Fields(0).Value'.

--  Phlip                  (no replies - address munged)
======= http://users.deltanet.com/~tegan/home.html =======
--  Programming Without Tan Lines, Inc.  --

- Show quoted text -

Quote:
>Thanks

Fri, 23 Feb 2001 03:00:00 GMT
Funky val() operation.

Quote:

> >I'm trying to take user input to create a mathematical function of a
> >variable (ex: "X^2" "-X + 2" run of the mill f(x)'s) by taking input
> >from a text box as a string, searching for the variable and replacing
> it
> >with the correct value.  I need it to return a numeric value though.
> So
> >I thought val() would work well.  It doesn't though.

> >For example,
> >val(1 + 2) ' would return 3, val(4^2 + 5) would return 21, and so on

> >But...
> >n = "1 + 2"
> >val(n) ' returns "1"
> >n = "4^2 + 5"
> >val(n) ' returns "4"

> >It seems to return the first number in the string.  Does anyone know
> how
> >to remedy this?  Is there a better way that I could go about
> >accomplishing my goal?

> You are asking for a "recursive descent parser". These have been written
> for VB; some small enough to fit in a VB Class, but there's another way.

> VB always has a database close at hand. Open a connection to it, and
> execute a line like this:

>     rs = db.OpenRecordset ("select 4^2 + 5")

> With a little fudging you should be able to beat 21 out of
> 'rs.Fields(0).Value'.

>   --  Phlip                  (no replies - address munged)
> ======= http://users.deltanet.com/~tegan/home.html =======
>   --  Programming Without Tan Lines, Inc.  --
> >Thanks

A parser is exactly what I'm trying to avoid writing :)
I'm not terribly experienced or skilled at using databases, how would I
go about doing what you were explaining?  I tried it, but I was getting
errors like object required (so i put a data control on the form) and
then got "object variable or with block variable not set"
It sounds like I need to have a database file to work with, but I'm not
sure how exactly to go about doing that (I don't have Access, but I do
have Excel 97), plus I'm not too keen on bringing a whole other file
into the picture.

Thanks again.

Fri, 23 Feb 2001 03:00:00 GMT
Funky val() operation.
You can't just use val to evaluate a string as a mathematical function :).
Writing a parser shouldn't be too hard....use recursion..if you want i could
write a sample one for you...but only if you need it, i don't have all the
time in the world :)

..Tummy..
ICQ: 1488812
Veridicus Software

Quote:

>I'm trying to take user input to create a mathematical function of a
>variable (ex: "X^2" "-X + 2" run of the mill f(x)'s) by taking input
>from a text box as a string, searching for the variable and replacing it
>with the correct value.  I need it to return a numeric value though.  So
>I thought val() would work well.  It doesn't though.

>For example,
>val(1 + 2) ' would return 3, val(4^2 + 5) would return 21, and so on

>But...
>n = "1 + 2"
>val(n) ' returns "1"
>n = "4^2 + 5"
>val(n) ' returns "4"

>It seems to return the first number in the string.  Does anyone know how
>to remedy this?  Is there a better way that I could go about
>accomplishing my goal?

>Thanks

Sat, 24 Feb 2001 03:00:00 GMT

 Page 1 of 1 [ 6 post ]

Relevant Pages