#2::
oldstr=(10*2)a5-16/8
StringReplace,newstr,oldstr,a,+,all
value:=newstr
tooltip,the values is %value%
return
displays "the value is (10*2)+5-16/8",but not "the value is 23.00000",why?
#2::
oldstr=(10*2)a5-16/8
StringReplace,newstr,oldstr,a,+,all
value:=newstr
tooltip,the values is %value%
return
Dude,
Don't you want...
#2::
oldstr:=(10*2)a5-16/8
StringReplace,newstr,oldstr,a,+,all
value = newstr
tooltip,the values is %newstr%
return
It looks like you but := in the wrong place.
Like this too...
#2::
oldstr := (10*2)a5-16/8
StringReplace,newstr,oldstr,a,+,all
value := newstr
tooltip,the values is %value%
return
oldstr:=(10*2)a5-16/8
msgbox,%oldstr% ;oldstr's values is 10*2=20
oldstr:=(10*2)a5-16/8gives you 20, because beyond a5 it is not a valid numeric expression any more. StringReplace will not find anything to change. But in any case the whole thing is useless, because AHK has not (yet) got dynamic expression evaluation. If you have an expression in a variable, you cannot directly evaluate it. You need more complex functions, like this one
#x:: ahkPath = "D:\Program Files\AutoHotkey\AutoHotkey.exe" ; tmppath is used both for a temp script and for the result. ; Strangely, quotes around tmpPath seems to break the file commands. tmpPath = C:\ahktmp.ahk oldStr = (10*2)a5-16/8 StringReplace newStr, oldStr, a, +, All startTime = %A_Now% loopCount = 1000 Loop %loopCount% { value := evaluate(newStr) } endTime = %A_Now% MsgBox, ( The value is %value%, calculated %loopCount% times from %startTime%` to %endTime%` ) return ; Evaluates any dynamic expression. ; On error, returns nothing and leaves ErrorLevel set. evaluate(string) { global ahkPath global tmpPath ifExist %tmpPath% { FileDelete %tmpPath% if ErrorLevel return } FileAppend, ; Create temp script ( #NoTrayIcon x:=%string% FileDelete %tmpPath% FileAppend `%x`%, %tmpPath% ), %tmpPath% if ErrorLevel return RunWait %ahkPath% %tmpPath%, , Min UseErrorLevel if ErrorLevel return FileRead result, %tmpPath% if ErrorLevel return FileDelete %tmpPath% Return result } ; Executes any dynamic script. ; On error, returns nothing and leaves ErrorLevel set. ; The second, optional parameter says whether to wait for it to ; finish executing or not. ; If not, the script must delete itself, and no value is returned. ; This is the default. ; Otherwise, if the script places anything into %outputString%, ; %outputString% is be returned. ; Otherwise, ErrorLevel will be returned. execute(string, waitForExec=0) { global ahkPath global tmpPath ifExist %tmpPath% { FileDelete %tmpPath% if ErrorLevel return } FileAppend, ; Create temp script ( #NoTrayIcon %string% FileDelete %tmpPath% if `%outputString`% FileAppend `%outputString`%, %tmpPath% else FileAppend ErrorLevel, %tmpPath% ), %tmpPath% if ErrorLevel return if %waitForExec% { RunWait %ahkPath% %tmpPath%, , Min UseErrorLevel if ErrorLevel return 0 FileRead result, %tmpPath% if ErrorLevel return 0 FileDelete %tmpPath% Return result } else Run %ahkPath% %tmpPath%, , Min UseErrorLevel }
tmpFile = %temp%\temp.ahk Str = 1+2*3 loopCount = 100 startTime = %A_TickCount% Loop %loopCount% value := Eval(Str) elapsedTime := A_TickCount - startTime MsgBox %Str% = %value%`n%loopCount% iterations take %elapsedTime% ms startTime = %A_TickCount% Loop %loopCount% value := EvalI(Str) elapsedTime := A_TickCount - startTime MsgBox %Str% = %value%`n%loopCount% iterations take %elapsedTime% ms return Eval(string) ; Evaluates a dynamic expression. { Global tmpFile FileDelete %tmpFile% FileAppend, ; Create temp script ( #NoTrayIcon x:=%string% FileDelete %tmpFile% FileAppend `%x`%, %tmpFile% ), %tmpFile% RunWait %tmpFile% ; Run AHK to execute temp script FileRead result, %tmpFile% FileDelete %tmpFile% Return result } EvalI(string) ; Evaluates Integer dynamic expression. { Global tmpFile FileDelete %tmpFile% ; Here will be the temp script written FileAppend #NoTrayIcon`nExit %string%, %tmpFile% RunWait %tmpFile% ; Run AHK to execute temp script Return %ErrorLevel% }Does anyone know, why?
Does anyone know, why?
startTime = %A_TickCount% loopCount = 100 Loop %loopCount% value := Eval(Str) elapsedTime := A_TickCount - startTime MsgBox, ( The value is %value% calculated %loopCount% times in %elapsedTime% ms ) startTime = %A_TickCount% ; <-- inserted code Loop %loopCount% value := EvalI(Str) elapsedTime := A_TickCount - startTime
tmpFile = %temp%\temp.ahk MsgBox % "Result = " Execute("ErrorLevel := 1+2*3", 1) MsgBox % Execute("a=1`nb := a+2*3`nErrorLevel = Result = %b%", 1) S = ; for variable reference during execution: % -> `% ( a = 1 b := a + 2*3 ErrorLevel = Result = `%b`% ) MsgBox % Execute(S,1) Execute("a := 1+2*3`nMsgBox Result = %a%") Execute(string, waitForExec=0) ; ErrorLevel is returned after executing string as AHK script { global tmpFile FileDelete %tmpFile% FileAppend, ; Create temp script ( #NoTrayIcon FileDelete %tmpFile% %string% FileAppend `%ErrorLevel`%, %tmpFile% ), %tmpFile% if %waitForExec% { RunWait %tmpFile% FileRead result, %tmpFile% FileDelete %tmpFile% Return result } else Run %tmpFile% }
EvalCB(string) ; Evaluates dynamic expression, Result -> ClipBoard { FileDelete $temp$.ahk ; Here will be the temp script written FileAppend #NoTrayIcon`nClipBoard:=%string%, $temp$.ahk RunWait $temp$.ahk ; Run AHK to execute temp script }A useful application is evaluating arithmetic expressions in documents. Select it, and the Win-Alt-e HotKey puts the result after it. Like, in a week there are 7*24*60 = 10080 minutes. Here " = 10080" was written by the script.
#!e:: ; Evaluates dynamic expression, Result -> ClipBoard Send ^c FileDelete $temp$.ahk ; Here will be the temp script written FileAppend #NoTrayIcon`nClipBoard:=%ClipBoard%, $temp$.ahk RunWait $temp$.ahk ; Run AHK to execute temp script Send {Right} = ^v ReturnIf you want, save the current ClipBoardAll in the beginning and restore it at the end, but then you have ugly side effects in MS Word and WordPerfect.
PROCESS_VM_OPERATION = 0x8 PROCESS_VM_WRITE = 0x20 pid := DllCall( "GetCurrentProcessId" ) VarSetCapacity( buffer, 100, 1 ) pBuffer := &buffer script = ( LTrim ` #NoTrayIcon hp_parent := DllCall( "OpenProcess" , "uint", %PROCESS_VM_OPERATION% | %PROCESS_VM_WRITE% , "int", false , "uint", %pid% ) DllCall( "WriteProcessMemory" , "uint", hp_parent , "uint", %pBuffer% , "uint", &expression , "uint", StrLen( expression )+1 , "uint", &written ) Exit, *( &written )+( *( &written+1 ) << 8 )+( *( &written+2 ) << 16 )+( *( &written+3 ) << 24 ) ) SetFormat, Float, 5.0 Random, num, 10000.0, 99999.0 temp_file = temp%num%.ahk FileDelete, %temp_file% expression = "1+1 = " 1+1 "``n2+2 = " 2+2 "``netc." FileAppend, expression := %expression%`n%script%, %temp_file% RunWait, %temp_file% ; required for binary data buffer_len := ErrorLevel FileDelete, %temp_file% MsgBox, %buffer% return
Wow!
add comments and explanations