Variable with Operator inside

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
euras
Posts: 429
Joined: 05 Nov 2015, 12:56

Variable with Operator inside

15 Oct 2018, 13:18

Hi, is it possible to set a value to variable using expresions, where the Operator is also into variable?

Code: Select all

var1 := "+1"
var2 := 5
var3 := var2 var1 ; should get 6
MsgBox % var3
User avatar
TheDewd
Posts: 1513
Joined: 19 Dec 2013, 11:16
Location: USA

Re: Variable with Operator inside

15 Oct 2018, 14:27

You can use the VBScript function Eval(), however it's only compatible with 32-bit AutoHotkey. You will be unable to use it with 64-bit AutoHotkey builds.

Code: Select all

#SingleInstance, Force

var1 := "+1"
var2 := 5
var3 := var2 var1

ScriptControl := ComObjCreate("ScriptControl")
ScriptControl.Language := "VBScript"
MsgBox, % ScriptControl.Eval(var3)
garry
Posts: 3770
Joined: 22 Dec 2013, 12:50

Re: Variable with Operator inside

15 Oct 2018, 14:43

eval from user Laszlo
https://autohotkey.com/board/topic/2411 ... evaluator/

Code: Select all

;- eval from user Laszlo 
;- https://autohotkey.com/board/topic/24114-popup-calculator-expression-evaluator/
v1 := "+1"
v2 := 5
v3 := eval(v2 v1)
msgBox, %v3%
return

;-------------------------------------
Eval(x)
{
   StringGetPos i, x, +, R
   StringGetPos j, x, -, R
   If (i > j)
      Return Left(x,i)+Right(x,i)
   If (j > i)
      Return Left(x,j)-Right(x,j)
   StringGetPos i, x, *, R
   StringGetPos j, x, /, R
   If (i > j)
      Return Left(x,i)*Right(x,i)
   If (j > i)
      Return Left(x,j)/Right(x,j)
   Return x
}
Left(x,i)
{
   StringLeft x, x, i
   Return Eval(x)
}
Right(x,i)
{
   StringTrimLeft x, x, i+1
   Return Eval(x)
}
;=== end function Laszlo ==========
User avatar
TheDewd
Posts: 1513
Joined: 19 Dec 2013, 11:16
Location: USA

Re: Variable with Operator inside

15 Oct 2018, 15:52

My attempt at combining the functions from Laszlo's approach into a single function:

Code: Select all

#SingleInstance, Force

MsgBox, % Eval("5+5")

Eval(x) {
	i := InStr(x, "+",, 0) - 1
	j := InStr(x, "-",, 0) - 1

	If (i > j) {
		return Eval(SubStr(x, 1, i)) + Eval(SubStr(x, 0, i + 1))
	}

	If (j > i) {
		return Eval(SubStr(x, 1, j)) - Eval(SubStr(x, 0, j + 1))
	}

	i := InStr(x, "*",, 0) - 1
	j := InStr(x, "/",, 0) - 1

	If (i > j) {
		return Eval(SubStr(x, 1, i)) * Eval(SubStr(x, 0, i + 1))
	}

	If (j > i) {
		return Eval(SubStr(x, 1, j)) / Eval(SubStr(x, 0, i + 1))
	}

	return x
}
garry
Posts: 3770
Joined: 22 Dec 2013, 12:50

Re: Variable with Operator inside

16 Oct 2018, 07:33

@TheDewd , thank you
here a calculator from LaBouche
https://autohotkey.com/board/topic/2523 ... -favorite/

Your example with inputbox

Code: Select all

#SingleInstance, Force

example=5*3
InputBox,c,calculate +-/*,Input> 4+3+5 ... etc, ,500,200,10,100,, Timeout,%example%
if c=
   return
if ErrorLevel
   return
d:=eval(c)
msgbox,%d%
return

;MsgBox, % Eval("5+5")
Eval(x) {
	i := InStr(x, "+",, 0) - 1
	j := InStr(x, "-",, 0) - 1
	If (i > j) {
		return Eval(SubStr(x, 1, i)) + Eval(SubStr(x, 0, i + 1))
	}
	If (j > i) {
		return Eval(SubStr(x, 1, j)) - Eval(SubStr(x, 0, j + 1))
	}
	i := InStr(x, "*",, 0) - 1
	j := InStr(x, "/",, 0) - 1
	If (i > j) {
		return Eval(SubStr(x, 1, i)) * Eval(SubStr(x, 0, i + 1))
	}
	If (j > i) {
		return Eval(SubStr(x, 1, j)) / Eval(SubStr(x, 0, i + 1))
	}
	return x
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], Joey5, just me and 194 guests