Push ini section into associative array? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
kunkel321
Posts: 1042
Joined: 30 Nov 2015, 21:19

Push ini section into associative array?

Post by kunkel321 » 23 Nov 2021, 18:04

I'm not sure why I'm struggling so much with arrays... I've found lots of examples, but I seem unable to adapt the code to my needs. Anyway, you guys can probably see what I'm trying to do here:

Code: Select all

myArray := []
varSection =
(
key1=My value for one.
key2=Another value for the second.
key3=And the value for the third key. 
)

loop, parse, varSection, `n
{
RegExMatch(A_Loopfield, "\w*(?==)", varKey)
RegExMatch(A_Loopfield, "=\K.*", varValue)
;MsgBox key:`t%varKey%`nvalue:`t%varValue%
myArray.Push(varKey, varValue)
}

MsgBox % myArray(key1)
I'm trying to get an ini section like this:

Code: Select all

key1=My value for one.
key2=Another value for the second.
key3=And the value for the third key. 
Into an associative array, like this:

Code: Select all

myArray := ["key1":"My value for one.","key2":"Another value for the second.","key3":"And the value for the third key."]
Thoughts?
ste(phen|ve) kunkel

sofista
Posts: 650
Joined: 24 Feb 2020, 13:59
Location: Buenos Aires

Re: Push ini section into associative array?

Post by sofista » 23 Nov 2021, 19:59

Maybe this can put you on the right track:

Code: Select all

myArray := {}
varSection =
(
key1=My value for one.
key2=Another value for the second.
key3=And the value for the third key. 
)

loop, parse, varSection, `n
{
	data := StrSplit(A_Loopfield, "=")
	myArray[data[1]] := data[2]
}

MsgBox % myArray["key1"]    ; My value for one.

iPhilip
Posts: 815
Joined: 02 Oct 2013, 12:21

Re: Push ini section into associative array?

Post by iPhilip » 23 Nov 2021, 22:35

Here is my thought:

Code: Select all

myArray := {}  ; Associative array
varSection =
(
key1=My value for one.
key2=Another value for the second.
key3=And the value for the third key. 
)

loop, parse, varSection, `n
if RegExMatch(A_Loopfield, "O)(\w*)=(.*)", var)
   myArray[var[1]] := var[2]

MsgBox % myArray.key1
Windows 10 Pro (64 bit) - AutoHotkey v2.0+ (Unicode 64-bit)

User avatar
Chunjee
Posts: 1418
Joined: 18 Apr 2014, 19:05
Contact:

Re: Push ini section into associative array?

Post by Chunjee » 24 Nov 2021, 03:39

for fun:

Code: Select all

A := new biga() ; requires https://www.npmjs.com/package/biga.ahk

myArray := {}  ; Associative array
varSection =
(
key1=My value for one.
key2=Another value for the second.
key3=And the value for the third key.
)

; separate all lines and split by = character
loop, parse, varSection, `n, `r
{
	myArray.push(strSplit(A_Loopfield, "="))
}
; cast arrays into key/value pairs
myArray := A.fromPairs(myArray)
; => {"key1": "My value for one.", "key2": "Another value for the second.", "key3": "And the value for the third key."}

https://biga-ahk.github.io/biga.ahk/#/?id=frompairs

User avatar
kunkel321
Posts: 1042
Joined: 30 Nov 2015, 21:19

Re: Push ini section into associative array?

Post by kunkel321 » 24 Nov 2021, 12:38

These are excellent -- Thanks Guys!!!
ste(phen|ve) kunkel

User avatar
kunkel321
Posts: 1042
Joined: 30 Nov 2015, 21:19

Re: Push ini section into associative array? And keep the Keys in order...

Post by kunkel321 » 25 Nov 2021, 13:18

Follow-up question:

Once I get the ini keys into the array, how do I preserve the order? I searched and was surprised.... It seems that there is not a "clean" way to do this(?) I suffixed A_Index to the beginning of A_LoopField, and that works until you get past nine elements, then they get out of order again. Like: 10,11,12,13,1,2,3,4,5,6,7,8,9.

You can see my super-kludgy solution. It seems to work, but is there a more elegant way I should preserve the order of the elements in the associative array?

Code: Select all

#NoEnv
#SingleInstance force
#Persistent

varSection =
(
Paste me=The five boxing wizards jump quickly and the quick brown fox jumps over the lazy dog, so pack my box with five dozen liquor jugs. 
Type me={Tab}{Tab}{Tab}
Paste me too=Lorem ipsum dolor sit amet, consectetur adipiscing elit stevano kunkoleti. Donec euismod odio sit amet maximus posuere. 
key4=val4
key5=val5
keySix=val6
key7=val7
key8=val8
keynine=vlanine
key10=val10
kayeleven=val11
keytwoleve=val12
key13=val13
)

myArray := {}

loop, parse, varSection, `n
{
	IndexSmall := A_Index / 100
    ThisPair := IndexSmall . " " . A_LoopField
    data := StrSplit(ThisPair, "=")
    MsgBox % "data1 " . data[1] . "`ndata2 " . data[2]
	myArray[data[1]] := data[2]
}
;MsgBox % myArray["key1"]    ; My value for one.

for item, thisKey in myArray
    MsgBox % thisKey

Return
ste(phen|ve) kunkel

iPhilip
Posts: 815
Joined: 02 Oct 2013, 12:21

Re: Push ini section into associative array?  Topic is solved

Post by iPhilip » 25 Nov 2021, 16:12

@kunkel321, The easiest way is to store each key/value pair into an associative array and store each one of those in a linear array. See below:

Code: Select all

#NoEnv
#SingleInstance force
#Persistent

varSection =
(
Paste me=The five boxing wizards jump quickly and the quick brown fox jumps over the lazy dog, so pack my box with five dozen liquor jugs. 
Type me={Tab}{Tab}{Tab}
Paste me too=Lorem ipsum dolor sit amet, consectetur adipiscing elit stevano kunkoleti. Donec euismod odio sit amet maximus posuere. 
key4=val4
key5=val5
keySix=val6
key7=val7
key8=val8
keynine=vlanine
key10=val10
kayeleven=val11
keytwoleve=val12
key13=val13
)

myArray := {}

loop, parse, varSection, `n
{
	IndexSmall := A_Index / 100
    ThisPair := IndexSmall . " " . A_LoopField
    data := StrSplit(ThisPair, "=")
    MsgBox % "data1 " . data[1] . "`ndata2 " . data[2]
	myArray[data[1]] := data[2]
}
;MsgBox % myArray["key1"]    ; My value for one.

for item, thisKey in myArray
    MsgBox % thisKey

; ---------------------------------------------

myArray := []

loop, parse, varSection, `n
{
    data := StrSplit(A_LoopField, "=")
	myArray.Push({key:data[1],value:data[2]})
}

for each, item in myArray
    MsgBox % item.key "=" item.value

Return
I hope this helps.
Windows 10 Pro (64 bit) - AutoHotkey v2.0+ (Unicode 64-bit)

User avatar
kunkel321
Posts: 1042
Joined: 30 Nov 2015, 21:19

Re: Push ini section into associative array?

Post by kunkel321 » 25 Nov 2021, 16:26

Ah yes -- That's much more sensible! Thanks iPhilip!
ste(phen|ve) kunkel

User avatar
kunkel321
Posts: 1042
Joined: 30 Nov 2015, 21:19

Re: Push ini section into associative array?

Post by kunkel321 » 26 Nov 2021, 13:49

Okay, here's a cool solution to a specific problem...

At work there is a web database that we have to build legal forms in. So I find myself needing to enter lots of boilerplate stuff into Chrome Browser web forms. SendEvent lets you visually see the strings getting typed, which is nice, but as we know, SendInput is preferred and is actually faster. In Chrome though, you have to just sit there and wait for the strings to get sent in the background (for what seems like forever), then the web page refreshes, and you can see your work. Pasting a string is--of course--much faster, but that doesn't let you jump to the next web form field via {Tab}. (Actually {Tab}{Tab} in this particular case). So the solution that you guys just helped make is to alternate between SendInput for the {Tab}{Tab} and paste for the rest.

I already use ini files to store my boilerplate text anyway, so now I can encode the key names with "Paste" or "Type." This sample ini file with a bunch of gibberish, has about 654 words. One of the web forms I often use has six different text fields.

With the "Paste me" keys as seen below, it takes less than one second to enter the strings. If you replace all of the occurrences of "Paste" with "Type," then it takes an average of 29 seconds--Quite a difference!

Spoiler
Spoiler
ste(phen|ve) kunkel

Post Reply

Return to “Ask for Help (v1)”