create ini file from associative array Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
gerard
Posts: 72
Joined: 01 Apr 2020, 13:14
Location: Buenos Aires
Contact:

create ini file from associative array

24 Jul 2021, 11:15

Hello, greetings to all.
When I run the following code, the ini file is created correctly, however the data is not in the same order in which it is in the array. Is there any way to fix this?

Code: Select all

hks := {"^+q": "exit"
	,"^right": "next"
	,"^left": "previous"
	,"^enter": "select"}

for k, v in hks
	iniWrite, %k%, config.ini, %v%, hk
resulting file:

[exit]
hk=^+q
[select]
hk=^enter
[previous]
hk=^left
[next]
hk=^right

I don't know if I'm missing something ...
If my tarzan english is not understood, let's blame google translate
User avatar
Smile_
Posts: 858
Joined: 03 May 2020, 00:51

Re: create ini file from associative array  Topic is solved

24 Jul 2021, 13:29

Use simple array:

Code: Select all

hks := [["^+q","exit"]
	   ,["^right","next"]
	   ,["^left","previous"]
	   ,["^enter","select"]]

for k, v in hks
	iniWrite, % v[1], config.ini, % v[2], hk
User avatar
boiler
Posts: 16925
Joined: 21 Dec 2014, 02:44

Re: create ini file from associative array

24 Jul 2021, 15:56

gerard wrote: however the data is not in the same order in which it is in the array.
...
I don't know if I'm missing something ...
That is not the order in which it is in the array. That is just the order you assigned them. The order you assign key/value pairs is not necessarily the order they will be retrieved. They are sorted on the keys, which is how they ended up: ^+,q, ^enter, ^left, ^right, with the character after the ^ being the consequential character: +, e, l, r. That's why Smile_'s approach of using a simple array keeps them in your desired order...because the keys are integers 1, 2, 3, 4, with each value being another (two-item) simple array.
gerard
Posts: 72
Joined: 01 Apr 2020, 13:14
Location: Buenos Aires
Contact:

Re: create ini file from associative array

24 Jul 2021, 15:58

Thanks for the reply. She had tried something more or less similar, but, but less verbose.
I think I will stick with the option you have proposed

Code: Select all

hks := ["^+q,Exit"
	   ,"^right,Next"
	   ,"^left,Previous"
	   ,"^enter,Select"]

for k, value in hks
    {
    data := strSplit(value, ",")
    iniWrite,% data[1], config.ini,% data[2], hk
    }
The data in the array will be written by people who do not know programming, so I was looking for something fairly intuitive to write.
Greetings
If my tarzan english is not understood, let's blame google translate

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Anput, mikeyww and 235 guests