Replacing variable with text based on variable content

Get help with using AutoHotkey and its commands and hotkeys
rjsutherland
Posts: 1
Joined: 19 Feb 2015, 19:24

Replacing variable with text based on variable content

19 Feb 2015, 19:34

Hi,
I'm fairly new to AHK so forgive me...
I have a variable that is going to come in as three numbers, and I want to replace that with text from a long pre-defined list. For instance,


I have an 80+ item list like this:
303 = ABC
404 = CBS
102 = CNN
512 = PBS
(etc)

%MyVar% is "303",
so I want AHK to go through that list and have %MyVar% become "ABC"

What's the best way to do this?
MJs
Posts: 454
Joined: 23 Sep 2014, 03:29

Re: Replacing variable with text based on variable content

19 Feb 2015, 20:16

if you can control your list, meaning you can change it at will, then create it as object:

Code: Select all

List:={303:"ABC",404:"CBS",102:"CNN",512:"PBS"}
MyVar = 303
if list.haskey(myvar)
	MsgBox % list[MyVar]
else
	MsgBox no item in the list
if you cannot control your list, meaning, for some reason you can't alter it to be an object:

Code: Select all

; keep in mind, this is given that the lines in the list are in this format:
; value space = space value end of line
list=
(
303 = ABC
404 = CBS
102 = CNN
512 = PBS
)
MyVar = 512
if RegExMatch(list, "`nm)^" myvar " = (.+)$", _)
	MsgBox % _1
else
	MsgBox no item in the list
Guest

Re: Replacing variable with text based on variable content

20 Feb 2015, 02:38

If the list is in the script you can do it directly

Code: Select all

303 = ABC
404 = CBS
102 = CNN
512 = PBS

MsgBox %303% 
MyVar=303
MsgBox % %MyVar%

Although MJs list solution would be the most elegant and safer solution I think
User avatar
empardopo
Posts: 336
Joined: 06 Oct 2013, 12:50
Location: Spain
Contact:

Re: Replacing variable with text based on variable content

20 Feb 2015, 02:52

Great examples!
I often think that many of these responses could be added as examples to the documentation of Autohotkey.

Thanks!
Everything is possible!

Return to “Ask For Help”

Who is online

Users browsing this forum: AHKStudent, boiler, nRec, RickC, Xtra and 127 guests