Reuse array Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Ecimeric
Posts: 130
Joined: 11 Jan 2017, 02:23

Reuse array

12 May 2021, 03:50

Code: Select all

Array := []
global AssocArray := {}
Loop, Read, MenuOne.txt
    Array.Push(StrSplit(A_LoopReadLine, ";"))
for index, element in Array {
    Menu, MenuOne, Add, % element.2, Function
    AssocArray[element.2] := element.3
}

Array := []
AssocArray := {}
Loop, Read, MenuTwo.txt
    Array.Push(StrSplit(A_LoopReadLine, ";"))
for index, element in Array {
    Menu, MenuTwo, Add, % element.2, Function
    AssocArray[element.2] := element.3
}

Function(ItemName, ItemPos) {
    Run, % prog.exe AssocArray[ItemName]
    return
}
How can I reuse AssocArray so that both calls to Function work? (At the moment, element.3 is empty for one.)
User avatar
mikeyww
Posts: 26869
Joined: 09 Sep 2014, 18:38

Re: Reuse array

12 May 2021, 06:40

You could use a different variable name for the second array.
Ecimeric
Posts: 130
Joined: 11 Jan 2017, 02:23

Re: Reuse array

12 May 2021, 11:17

I had tried that, but element.3 is still empty if I change Array to ArrayB.
User avatar
mikeyww
Posts: 26869
Joined: 09 Sep 2014, 18:38

Re: Reuse array

12 May 2021, 14:45

What are the two files?
Ecimeric
Posts: 130
Joined: 11 Jan 2017, 02:23

Re: Reuse array

12 May 2021, 15:04

Both files look like —

Code: Select all

;Arrays;https://autohotkey.com/docs/Arrays
— but I only ever see Arrays with MsgBox.
User avatar
mikeyww
Posts: 26869
Joined: 09 Sep 2014, 18:38

Re: Reuse array  Topic is solved

12 May 2021, 17:59

Code: Select all

dir = %A_ScriptDir%
Global AssocArray := {}
read(dir "\MenuOne.txt")
read(dir "\MenuTwo.txt")
Menu, MenuOne, Show
; Menu, MenuTwo, Show

read(file) {
 SplitPath, file,,,, menuName
 Loop, Read, %file%
 { element := StrSplit(A_LoopReadLine, ";")
   Menu, %menuName%, Add, % element.2, browse
   AssocArray[menuName, element.2] := element.3
 }
}

browse(ItemName, ItemPos, MenuName) {
 Run, % "chrome.exe " AssocArray[MenuName, ItemName]
}
Ecimeric
Posts: 130
Joined: 11 Jan 2017, 02:23

Re: Reuse array

12 May 2021, 20:54

Thank you; the menus appear to be working as expected.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 302 guests