Jump to content

How to evaluate a string ?

Recommended Posts

Hi all !

Here is my problem : I want to evaluate a string but I can't make it work with Eval() or Number(). Here is an example :

$a = 2
$b = 3
$eq = "a^b"

$eq = StringReplace($eq,"a",$a)
$eq = StringReplace($eq,"b",$b)
$res = Number($eq)

msgbox(0,"Evaluation",$eq & "=" & $res);will produce 2^3=2

The problem is that the number() fonction automatically stops when a character is detected, like the "^" in "2^3"

Number(2^3) will produce the right answer (8) but Number("2^3") equals Number("2") = 2

Anyone know how to correctly evaluate a random equation ?

Thanks a lot in advance

Link to comment
Share on other sites

Eval and Number probably do not work the way you expect; you cannot evaluate an arithmetic expression with those functions. Look at the documentation examples for those functions to see their intended use.

Here's what you need to do:

1) Write out the string to a secondary AutoIt script that runs.

2) The secondary AutoIt script writes its output to a temp file.

3) Your main script gets the value from the temp file and deletes the secondary script and temp file

$a = 2
$b = 3
$eq = $a & "^" & $b

$AUTOIT = '"C:\Program Files\AutoIt3\AutoIt3.exe"'
FileWriteLine("C:\tempEval.au3", '$result = ' & $eq)
FileWriteLine("C:\tempEval.au3", 'FileWriteLine("C:\tempResult.txt", $result)')
RunWait($AUTOIT & "C:\tempEval.au3")
$eval = FileReadLine("C:\tempResult.txt")

MsgBox(4096,"", $eval)
Edited by CyberSlug
Use Mozilla | Take a look at My Disorganized AutoIt stuff | Very very old: AutoBuilder 11 Jan 2005 prototype I need to update my sig!
Link to comment
Share on other sites

Thanks ! It seems to work !

But it's a pity that there's no other way to do this arithmetic evaluation...

Eval() or Number() should do the job...

I'm going to use this script to find an answer to a random equation... So I need to brute force it...

With so much things to do for each test (multiple nested for 0 to 9 next) my script won't be very effective and way to sloooooooooow :D

Anyway thanks for your help !

Link to comment
Share on other sites

I have Exec on my to do list. It will determine a value using variables, operators and certain functions. It will not allow you to call potentially destructive functions, like RegDelete(), but will allow purely math functions like Sin(). It is a little ways down the list, after a bunch of things, like :ph34r: work. :(

David Nuttall
Nuttall Computer Consulting

An Aquarius born during the Age of Aquarius

AutoIt allows me to re-invent the wheel so much faster.

I'm off to write a wizard, a wonderful wizard of odd...

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...