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:
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)
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.
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)
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."}
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?
#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
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!
[section]
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}
Paste me too=Lorem ipsum dolor sit amet, consectetur adipiscing elit stevano kunkoleti. Donec euismod odio sit amet maximus posuere. Lorem ipsum dolor sit amet, consectetur adipiscing elit stevano kunkoleti. Donec euismod odio sit amet maximus posuere. Nullam eu mi turpis. Aliquam placerat enim at erat sodales consectetur. Curabitur porta pulvinar est, quis condimentum nunc tristique ac. Maecenas malesuada maximus scholaru et psycholica turpis at assessmo. Pellentesque ac sapien egestas verbalo comprohendo, posuere risus sed, porta erat. Mauris pharetra lobortis massa vel tristique. Fusce hendrerit ligula vitae sem lobortis dapibus. Fontono lobototo donec eget ornare velit, in mattis felis. Fusce eros libero, vehicula eget pulvinar id, posuere et Woodcolus Johnseni lando Whechsler lectus es intelligencio. Nulla in magna a odio venenatis till et finito.
Type me={Tab}{Tab}
Paste me too=Lorem ipsum dolor sit amet, consectetur adipiscing elit stevano kunkoleti. Donec euismod odio sit amet maximus posuere. Lorem ipsum dolor sit amet, consectetur adipiscing elit stevano kunkoleti. Donec euismod odio sit amet maximus posuere. Nullam eu mi turpis. Aliquam placerat enim at erat sodales consectetur. Curabitur porta pulvinar est, quis condimentum nunc tristique ac. Maecenas malesuada maximus scholaru et psycholica turpis at assessmo. Pellentesque ac sapien egestas verbalo comprohendo, posuere risus sed, porta erat. Mauris pharetra lobortis massa vel tristique. Fusce hendrerit ligula vitae sem lobortis dapibus. Fontono lobototo donec eget ornare velit, in mattis felis. Fusce eros libero, vehicula eget pulvinar id, posuere et Woodcolus Johnseni lando Whechsler lectus es intelligencio. Nulla in magna a odio venenatis till et finito.
Type me={Tab}{Tab}
Paste me too=Lorem ipsum dolor sit amet, consectetur adipiscing elit stevano kunkoleti. Donec euismod odio sit amet maximus posuere. Lorem ipsum dolor sit amet, consectetur adipiscing elit stevano kunkoleti. Donec euismod odio sit amet maximus posuere. Nullam eu mi turpis. Aliquam placerat enim at erat sodales consectetur. Curabitur porta pulvinar est, quis condimentum nunc tristique ac. Maecenas malesuada maximus scholaru et psycholica turpis at assessmo. Pellentesque ac sapien egestas verbalo comprohendo, posuere risus sed, porta erat. Mauris pharetra lobortis massa vel tristique. Fusce hendrerit ligula vitae sem lobortis dapibus. Fontono lobototo donec eget ornare velit, in mattis felis. Fusce eros libero, vehicula eget pulvinar id, posuere et Woodcolus Johnseni lando Whechsler lectus es intelligencio. Nulla in magna a odio venenatis till et finito.
Type me={Tab}{Tab}
Paste me too=Lorem ipsum dolor sit amet, consectetur adipiscing elit stevano kunkoleti. Donec euismod odio sit amet maximus posuere. Lorem ipsum dolor sit amet, consectetur adipiscing elit stevano kunkoleti. Donec euismod odio sit amet maximus posuere. Nullam eu mi turpis. Aliquam placerat enim at erat sodales consectetur. Curabitur porta pulvinar est, quis condimentum nunc tristique ac. Maecenas malesuada maximus scholaru et psycholica turpis at assessmo. Pellentesque ac sapien egestas verbalo comprohendo, posuere risus sed, porta erat. Mauris pharetra lobortis massa vel tristique. Fusce hendrerit ligula vitae sem lobortis dapibus. Fontono lobototo donec eget ornare velit, in mattis felis. Fusce eros libero, vehicula eget pulvinar id, posuere et Woodcolus Johnseni lando Whechsler lectus es intelligencio. Nulla in magna a odio venenatis till et finito.
Type me={Tab}{Tab}
Paste me too=Lorem ipsum dolor sit amet, consectetur adipiscing elit stevano kunkoleti. Donec euismod odio sit amet maximus posuere. Lorem ipsum dolor sit amet, consectetur adipiscing elit stevano kunkoleti. Donec euismod odio sit amet maximus posuere. Nullam eu mi turpis. Aliquam placerat enim at erat sodales consectetur. Curabitur porta pulvinar est, quis condimentum nunc tristique ac. Maecenas malesuada maximus scholaru et psycholica turpis at assessmo. Pellentesque ac sapien egestas verbalo comprohendo, posuere risus sed, porta erat. Mauris pharetra lobortis massa vel tristique. Fusce hendrerit ligula vitae sem lobortis dapibus. Fontono lobototo donec eget ornare velit, in mattis felis. Fusce eros libero, vehicula eget pulvinar id, posuere et Woodcolus Johnseni lando Whechsler lectus es intelligencio. Nulla in magna a odio venenatis till et finito. 654 words total.