Alternative to iniread

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Vh_
Posts: 203
Joined: 17 Mar 2017, 22:06

Alternative to iniread

20 Feb 2019, 18:51

Hello,

I've got a script that gets an index from a website, and then utilizes a .ini file to convert that number 1-50 into a US state code. I'd like to put it into the script to avoid an external file, however it seems a little tedious to do 50 If statements..such as this:

Code: Select all

If (Number = 1)
    State = AK
If (Number = 2)
   State = AZ
If (Number = 3)
   State = AR
If (Number = 4)
.......
Seems like this is not an efficient way to do this. Any ideas on an alternative?

Thanks,
Vh_
gregster
Posts: 9095
Joined: 30 Sep 2013, 06:48

Re: Alternative to iniread

20 Feb 2019, 18:59

How about an associative array?

Code: Select all

states := {1: "AK", 2: "AZ", 3: "AR", 4 : "XY" }

i := 3
msgbox % states[i]	; state with index 3

;enumerate all states
for nr, state in states
	msgbox % nr " : "  state
Normal array would work, too, in this case (then, you'd have an implicit index).
Qriist
Posts: 82
Joined: 11 Sep 2016, 04:02

Re: Alternative to iniread

20 Feb 2019, 19:18

dang, just came in to say that.

For readability you can do

Code: Select all

states := {1: "AK"
	, 2: "AZ"
	, 3: "AR"
	, 4 : "XY" }
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Alternative to iniread

20 Feb 2019, 19:53

Here are two ways. Cheers.

Code: Select all

oArray := ["AK", "AZ", "AR"]
vNum := 3
MsgBox, % oArray[vNum]

oArray := StrSplit("AK,AZ,AR", ",")
vNum := 3
MsgBox, % oArray[vNum]
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Vh_
Posts: 203
Joined: 17 Mar 2017, 22:06

Re: Alternative to iniread

21 Feb 2019, 23:37

Thanks all for the responses! I'll give this a test tomorrow and let you know how it turns out.
User avatar
evilC
Posts: 4823
Joined: 27 Feb 2014, 12:30

Re: Alternative to iniread

22 Feb 2019, 05:36

If you want to load this array from a settings file, you probably don't want to use INIRead / INIWrite.
I would use Json
Then you can just store the array verbatim in the JSON file.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], Luke10 and 91 guests