ezzetabi Posted September 10, 2004 Share Posted September 10, 2004 (edited) 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 September 10, 2004 by ezzetabi Link to comment Share on other sites More sharing options...
SlimShady Posted September 10, 2004 Share Posted September 10, 2004 I hope you developers can make this happen... Link to comment Share on other sites More sharing options...
Developers Jos Posted September 10, 2004 Developers Share Posted September 10, 2004 (edited) 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 September 10, 2004 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 More sharing options...
ezzetabi Posted September 11, 2004 Author Share Posted September 11, 2004 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... Link to comment Share on other sites More sharing options...
Matt @ MPCS Posted September 11, 2004 Share Posted September 11, 2004 I too would like to see this available. AutoIt understands calling function names within variables so I don't know why it wouldn't be possible. *** Matt @ MPCS Link to comment Share on other sites More sharing options...
SlimShady Posted September 11, 2004 Share Posted September 11, 2004 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 More sharing options...
this-is-me Posted September 11, 2004 Share Posted September 11, 2004 (edited) 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 September 11, 2004 by this-is-me Who else would I be? Link to comment Share on other sites More sharing options...
Matt @ MPCS Posted September 11, 2004 Share Posted September 11, 2004 @ this-is-meI 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! *** Matt @ MPCS Link to comment Share on other sites More sharing options...
Administrators Jon Posted September 12, 2004 Administrators Share Posted September 12, 2004 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. Deployment Blog: https://www.autoitconsulting.com/site/blog/ SCCM SDK Programming: https://www.autoitconsulting.com/site/sccm-sdk/ Link to comment Share on other sites More sharing options...
Administrators Jon Posted September 12, 2004 Administrators Share Posted September 12, 2004 @ this-is-me I believe you can already do the: Nah, Eval() just looks up variable names, it can't handle functions. Deployment Blog: https://www.autoitconsulting.com/site/blog/ SCCM SDK Programming: https://www.autoitconsulting.com/site/sccm-sdk/ Link to comment Share on other sites More sharing options...
Matt @ MPCS Posted September 12, 2004 Share Posted September 12, 2004 @Jon I really think it would be very useful if it can be done. Let me know if there is anything I can do to help. *** Matt @ MPCS Link to comment Share on other sites More sharing options...
Valik Posted September 12, 2004 Share Posted September 12, 2004 Here is one way to do it... THIS MUST BE COMPILED!$needsEvaluated = "1*2+3" $res = RunWait(@ScriptFullPath & ' /c "Exit ' & $needsEvaluated & '"') MsgBox(4096, "", $res) Link to comment Share on other sites More sharing options...
Administrators Jon Posted September 12, 2004 Administrators Share Posted September 12, 2004 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. Deployment Blog: https://www.autoitconsulting.com/site/blog/ SCCM SDK Programming: https://www.autoitconsulting.com/site/sccm-sdk/ Link to comment Share on other sites More sharing options...
this-is-me Posted September 13, 2004 Share Posted September 13, 2004 @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. @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 More sharing options...
Valik Posted September 13, 2004 Share Posted September 13, 2004 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 More sharing options...
Matt @ MPCS Posted September 13, 2004 Share Posted September 13, 2004 @ this-is-meYes 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 More sharing options...
this-is-me Posted September 13, 2004 Share Posted September 13, 2004 (edited) @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 September 13, 2004 by this-is-me Who else would I be? Link to comment Share on other sites More sharing options...
Valik Posted September 13, 2004 Share Posted September 13, 2004 (edited) 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 September 13, 2004 by Valik Link to comment Share on other sites More sharing options...
this-is-me Posted September 13, 2004 Share Posted September 13, 2004 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 More sharing options...
NewKreation Posted November 24, 2004 Share Posted November 24, 2004 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?ORHas anyone found an alternate solution?$x = 2 Exp[b][/b]ression = "$x*38" $i = Calc ("Exp[b][/b]ression") ; $i = 76Thanks! Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now