Jump to content

Making calculation inside a string.


ezzetabi
 Share

Recommended Posts

I have a string that contains values, operations and 'codes'.

I need to replace the code with some values and calculate the results...

I tried Number() and Int() but, no luck.

E.g.

$i = '5 * code + 4'

$i =StringReplace($i,'code',7)

$i = Number($i)

Edited by ezzetabi
Link to comment
Share on other sites

  • Developers

I have a string that contains values, operations and 'codes'.

I need to replace the code with some values and calculate the results...

I tried Number() and Int() but, no luck.

E.g.

$i = '5 * code + 4'

$i =StringReplace($i,'code',7)

$i = Number($i)

<{POST_SNAPBACK}>

What about writing your own UDF?

Heres a start with the basics, but doesn't take calculation rules into account, just a straight forward left to right calculation:

$i = '5 * code + 4'
$i = StringReplace($i, 'code', 7)
$i = Convert($i)
MsgBox(4096, 'debug:', '$i:' & $i);### Debug MSGBOX 


Func Convert($i_STr)
   $parts = StringSplit($i_STr, " ")
   $result = $parts[1]
   For $x = 2 To $parts[0]
      Select
         Case $parts[$x] = "*"
            $result = $result * $parts[$x + 1]
         Case $parts[$x] = "/"
            $result = $result / $parts[$x + 1]
         Case $parts[$x] = "-"
            $result = $result - $parts[$x + 1]
         Case $parts[$x] = "+"
            $result = $result + $parts[$x + 1]
      EndSelect
   Next
   Return $result
EndFunc  ;==>Convert
Edited by JdeB

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Well.

I can make my udf, but considering how many operations exist and the complex rules it is quite hard. Isn't it?

And this indeed sounds like reinvent the wheel since AutoIt already understand this things...

<{POST_SNAPBACK}>

Exactly.

What I was thinking:

There should be a function that processes a string as an mathematical calculation.

Math("(3 + 5)  * 8")

or

Calc("(3 + 5)  * 8")
Link to comment
Share on other sites

I believe that the eval function should be expanded to include this and other things:

Eval("somefunction('test')")

$num = Eval("(3 + 5)  * 8"))

for $i = 0 to 5
    $var = Eval("$num" & $i)
    MsgBox(0,"",$var)
next
Edited by this-is-me
Who else would I be?
Link to comment
Share on other sites

@ this-is-me

I believe you can already do the:

Eval("somefunction('test')")

This is accomplished using the Call() function. According to the documentation, the Call() function:

Calls a user-defined function contained in a string parameter.

It almost does what you are talking about, but it cannot take a built-in AutiIt function, and cannot pass parameters... but hey its a start! :ph34r:

*** Matt @ MPCS

Link to comment
Share on other sites

  • Administrators

Instead of writing a new function that evaluates an expression it would probably be easier to add an uber function that executes any line of autoit code.

e.g.

$command = "$var = 1 * 2 + somefunc()"
AutoItParse($command)
MsgBox(0, "Value of $var is", $var)

I think that could be done fairly easily due to the way AutoIt is already setup.

I think.

Link to comment
Share on other sites

  • Administrators

Here is one way to do it... THIS MUST BE COMPILED!

$needsEvaluated = "1*2+3"
$res = RunWait(@ScriptFullPath & ' /c "Exit ' & $needsEvaluated & '"')
MsgBox(4096, "", $res)

That's not just outside the box, that's in a different state. :ph34r:
Link to comment
Share on other sites

@matt, no. Call only calls functions with no parameters, the eval would (in my dreams) evaluate any expression.

@jon, valik found the box and is trying to get back in it, but he doesn't fit. :ph34r:

@valik, I love it. The only problem I see with that is when it comes to functions. The functions would not be aware of the scope or of any variables in the original script.

@jon again, I am requesting that the eval function be able to parse a line just like autoit natively would. Is this even possible?

Who else would I be?
Link to comment
Share on other sites

this-is-me, you don't seem to understand... There are no variables in what I did, nor is there any scope or functions or anything. I used a little known feature of AutoIt. I can replace @ScriptFullPath with the path to AutoIt3.exe or any other compiled AutoIt script on my PC to get the same effect.

Calling any compiled script with /c "any valid AutoIt command" will send AutoIt into single-line mode where it only processes the line passed to it on the command line. It just so happens, there isn't any real limitation on what command can be passed (Except that it must fit on a single line), so I'm effectively telling AutoIt to enter single-line mode and immediately exit. The key is, by returning the equation that needs solved as the exit code, it's processed and the result become the exit code, which the call to RunWait captures, thus, bringing it back into the original script.

As for Eval() doing similar, that is outside the scope of Eval(). Eval() and Assign() are more like poor-man's pointers. The two combined allow full emulation of pointers. That means, they are only related to variables; specifically, the creation/resolution of them.

Link to comment
Share on other sites

@ this-is-me

Yes I realize that the call function cannot pass parameters, that is why I added:

It almost does what you are talking about, but it cannot take a built-in AutiIt function, and cannot pass parameters... but hey its a start!

*** Matt @ MPCS

Link to comment
Share on other sites

@valik, Yes. I perfectly understood what you did [i knew about this feature before], but I am only saying that your workaround wil not help in the situation I need. I AM saying that the eval function needs to be completely rewritten so that when a line of text is sent to eval, it "Evaluates" that expression just as autoit would evaluate that expression if it was inside an au3. The only difference is it would be evaluated while running inside of the host script. The only reason I said what I did is to let you know that it is not a valid workaround for what I need.

Edited by this-is-me
Who else would I be?
Link to comment
Share on other sites

What I posted was for ezzetabi's need, anyway, since it is only possible to return a numeric exit code. I never claimed it would solve all problems.

What you are asking for seems more of a way to do dynamic code generation. If that's the case, you'll have to have a pretty convincing argument as to why that should be possible. I already see people mis-using Call when it's toally unnecessary; I don't really want to see yet another way for new people to shoot themselves in the foot, having something as powerful as dynamic code generation isn't just "shooting yourself in the foot", that goes into the "blowing your leg off" realm.

Edited by Valik
Link to comment
Share on other sites

Sorry, I can't seem to think of anything off the top of my head. I have had a very great need for this before, but I can't remember where. As soon as I think of it, I will bring the subject back up.

Let me say, however, that your funny comments left me sitting here going "Even if I had a valid reason I might not post it". You have brought a smile to my face. Thank you.

Who else would I be?
Link to comment
Share on other sites

  • 2 months later...

This is exactly what I am trying to do. Duh! :) That is how I came upon this thread. But I digress...

I would like to be able to store expressions (with variables) in an ini file and recall them later. This new feature would solve all my problems.

Is this something that can be added to AutoIt relatively easily?

OR

Has anyone found an alternate solution?

$x = 2
Exp[b][/b]ression = "$x*38"
$i = Calc ("Exp[b][/b]ression") ; $i = 76

Thanks!

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
 Share

  • Recently Browsing   0 members

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