dealing with no Literal assignments in AHK v2

Get help for the alpha version of AutoHotkey v2 here. Please state the v2 version you are working with in the title when making a new topic.
User avatar
TheArkive
Posts: 49
Joined: 05 Aug 2016, 08:06

dealing with no Literal assignments in AHK v2

16 Jan 2020, 07:38

I'm trying to come up with my own method of dealing with liter assignments being removed, and prepping current and future code. I'm wondering if what I came up with will be prone to failure.

I'm trying to use `` in place of ". It appears to work below as long as that is the first thing I address (and replace). Would that be prone to failure in AHK v2 for any known reason?

======== EDIT ========
trying to get rid of my mostly pointless library functions... found a more organic solution, but also found some weirdness.
Below code should work, I'll post again with the weirdness I found...

oy, just me learning how "If var is Type" works ... aparently 2.0 is not considered an integer, upon reading the docs I see why
...
function update, more organic, and should work

Code: Select all

test1 := "1234"
test2 := "5678"
test3 := "9012"
phrase := "abcd %test1% efgh ``%test2%`` ijkl %test3% mnop %loc1% %loc2%"

MyFunc(phrase)

myFunc(phrase) {
	loc1 := "qrs", loc2 := "tuv"			; local vars declared in the function
	
	params := Map("loc1",loc1,"loc2",loc2)	; prep object to pass local vars
	
	test := eval(phrase,params*)			; pass local vars into eval() function
	msgbox % test
	
	test := eval(phrase)					; local params are optional, all global vars are processed
	msgbox % test							; of course %var% will remain if not it's local and not in params
}


eval(sInput,params*) {
	sInput := StrReplace(sInput,"``",Chr(34))	; replace `` with "
	array := StrSplit(sInput,Chr(37))
	
	For k, v in array {
		q := Mod(A_Index,2)						; even index are the variables
		If (q) {
			sOutput .= v
		} Else {
			t := %v%							; translate variable (which can be global or built in)
			If (t)
				sOutput .= t					; if matched, replace
			Else
				sOutput .= Chr(37) v Chr(37)	; else put it back ; chr(37) = "%"
		}
	}
	
	For index,param in params {
		rpl := Chr(37) index Chr(37)
		sOutput := StrReplace(sOutput,rpl,param)
	}
	
	return sOutput
}

Map(params*) {
	return Object(params*)
}

ExitApp
Other than that, would anyone find this useful?

==============================================================

EDIT: One thing that would make this more intuitive, would be for variadic functions to be able to store the name of the var passed into the param, then we can use:

Code: Select all

EDIT: oops, this is what i meant

value1 := 1
value2 := 2
value3 := 3

 array := [value1,value2,value3]
... and still get the name of the var as ... maybe a property? And perform the same kind of substitution.

I'm thinking of an implementation like this:

Code: Select all

Join(sep, params*) {
    for index,param in params
        ; index = index
        ; param.name = var name passed into the params* array at %index%
        ; param.value = value of param at %index%
    return ...
}
not sure if that would tank performance, but it sure would be marginally convenient. If I can remove a single character from 100+ different lines of code, I do it :P
guest3456
Posts: 2927
Joined: 09 Oct 2013, 10:31

Re: dealing with no Literal assignments in AHK v2

16 Jan 2020, 11:04

i dont understand what you're trying to accomplish. ahk v2 used to allow %derefs% inside of quoted strings, but it was removed. why not just use regular concatenation like you're supposed to?

User avatar
TheArkive
Posts: 49
Joined: 05 Aug 2016, 08:06

Re: dealing with no Literal assignments in AHK v2

16 Jan 2020, 11:49

When I try to compose complex command line strings with "quotes" inside "quotes" I find the %var% substitution quite convenient. Just trying to find a reasonable way to keep that... I find it helps readability (for me).
User avatar
TheArkive
Posts: 49
Joined: 05 Aug 2016, 08:06

Re: dealing with no Literal assignments in AHK v2

16 Jan 2020, 12:54

@guest3456

What I'm trying to accoplish, is to have something that is close to litral assignments so I don't have as much work to do when converting my scripts. And, i find the literal assignments to be helpful for improving readability, particularly, when composing certain command line strings.

Not sure if the way I said it previously came across this way.... so i tried to clarify.

Return to “AutoHotkey v2 Help”

Who is online

Users browsing this forum: No registered users and 3 guests