Keyword replace from map?

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
Coiler
Posts: 114
Joined: 29 Nov 2020, 09:06

Keyword replace from map?

08 Apr 2021, 14:12

What would be the easiest way to set up a replacement process where a user-defined string-map replaces keywords in a string?

Here's an example of what I want. I'm throwing this out on the spot, so apologies for syntax mishaps:

Code: Select all

keys := map()
keys["MAXBUF"] := 4000
keys["MINBUF"] := 100

ustring := "MyValue = MAXBUF;\nAltValue = MINBUF;"
ustring := replaceKeys( ustring, keys )
ustring result: "MyValue = 4000;\nAltValue = 100"
I'm new to using AHK regex - does it support anything like this? Or would it be better to parse the string myself?

Appreciate any advice!
User avatar
boiler
Posts: 16926
Joined: 21 Dec 2014, 02:44

Re: Keyword replace from map?

08 Apr 2021, 14:41

This seems easiest to me:

Code: Select all

keys := map()
keys["MAXBUF"] := 4000
keys["MINBUF"] := 100

ustring := "MyValue = MAXBUF;\nAltValue = MINBUF;"
for k, v in keys
	ustring := StrReplace(ustring, k, v)
MsgBox ustring ; ustring result: "MyValue = 4000;\nAltValue = 100;"

Or so the main part of your code would look exactly as you have it:

Code: Select all

keys := map()
keys["MAXBUF"] := 4000
keys["MINBUF"] := 100

ustring := "MyValue = MAXBUF;\nAltValue = MINBUF;"
ustring := replaceKeys( ustring, keys )
MsgBox ustring ; ustring result: "MyValue = 4000;\nAltValue = 100;"
return

replaceKeys(str, keys) {
	for k, v in keys
		str := StrReplace(str, k, v)
	return str
}

Coiler wrote: I'm new to using AHK regex - does it support anything like this? Or would it be better to parse the string myself?
RegEx can do this, but it's not necessary. StrReplace is preferred when it is capable.
User avatar
Coiler
Posts: 114
Joined: 29 Nov 2020, 09:06

Re: Keyword replace from map?

08 Apr 2021, 15:32

Thanks for your response! My main concern is that the user string will be an entire HTML page. I probably should have mentioned this. What you suggest would still work, but I was hoping for something where the main string would only need parsed once. I can enforce some type of prefix on variable names if I have to, such as @MAXBUF.

I guess regex would make it pretty simple once I add a prefix. Or I guess I could auto-generate the input string by appending all of the keys together, such as (Var1|Var2|Var3|Var4|etc). But then I'm assuming AHK needs to loop through some list to check for these, then they would just get chucked into the map, regardless. The prefix would probably be a lot more efficient.

Edit: For some reason, I'm assuming AHK regex allows using some type of closure for the replacement string, but I'm pretty sure that's not the case. That means I probably have to use that Callout system, which I know nothing about.
User avatar
boiler
Posts: 16926
Joined: 21 Dec 2014, 02:44

Re: Keyword replace from map?

08 Apr 2021, 15:50

I was not able to find a way in which you could choose which replacement to use based on which of (Var1|Var2|Var3|Var4|etc) is found (i.e., have RegEx use a replacement dictionary). I was looking at the Callout approach, but I don't see where it allows you to specify what to replace it with. You can just return different values telling it whether or not the match is considered a success and whether or not to proceed looking for other matches. That doesn't mean there aren't tricks that I may be missing. But it doesn't seem to me that any of that would be more efficient than sequential StrReplace() calls.
User avatar
Coiler
Posts: 114
Joined: 29 Nov 2020, 09:06

Re: Keyword replace from map?

08 Apr 2021, 16:47

Yeah, after thinking about all of the possible methods that could be used to accomplish this, I've started appreciating your first approach a lot more. Any type of parsing system that needs to check for 10+ keywords is not going to be much more efficient than just repeating the process for each keyword. It may even be less efficient.

I suppose I could limit keyword replacements to attribute assignment values in the HTML (like <tag event="|{Space down}|DoSomething()|Wait(33)|{Space up}|">), since that is really the only valid place the user can add them in.

So I'm probably either going to do a separate call for each one, or require keywords to use some operator prefix, and parse the string myself with ToStr() or etc.

By the way, thanks for letting me know about the Callout system. I assumed it allowed you to replace the substrings with a return value of some sort.

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: Albireo, CraigM, Panaku, RussF, wpulford and 31 guests