Separate menus seen as one 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

Separate menus seen as one

17 May 2021, 18:11

Code: Select all

menuitems.txt
MySubmenu;Test;Result 1
;Test;Result 2

global AssocArray := {}
Array := []
Loop, Read, menuitems.txt
    Array.Push(StrSplit(A_LoopReadLine, ";"))
for index, element in Array {
    if (element.1 > "") {
        Menu, % element.1, Add, % element.2, Function
        Menu, MyMenu, Add, % element.1, % ":" element.1
    } else {
        Menu, MyMenu, Add, % element.2, Function
    }
    AssocArray[element.2] := element.3
}
Menu, MyMenu, Show

Function(ItemName, ItemPos) {
    MsgBox, % AssocArray[ItemName]
    return
}
In MySubmenu, why does Test return Result 2 instead of Result 1? AHK appears to be interpreting two identical menu item names as being in the same menu.
Last edited by Ecimeric on 18 May 2021, 01:42, edited 2 times in total.
User avatar
mikeyww
Posts: 26869
Joined: 09 Sep 2014, 18:38

Re: Separate menus seen as one

17 May 2021, 18:46

Because your AssocArray overwrote its value. Each key in an array is unique, not duplicated.
Ecimeric
Posts: 130
Joined: 11 Jan 2017, 02:23

Re: Separate menus seen as one

17 May 2021, 19:30

How do I need to change (presumably) AssocArray[element.2] := element.3 to prevent that?
User avatar
boiler
Posts: 16911
Joined: 21 Dec 2014, 02:44

Re: Separate menus seen as one

17 May 2021, 19:49

It’s not necessarily that the line AssocArray[element.2] := element.3 needs to change. In both lines of your text file, the second field is test, which is what you’re choosing as keys to your associative array. If you want them to be different, change one of them from test to something else so that each item of your associative array will have a different key.
Ecimeric
Posts: 130
Joined: 11 Jan 2017, 02:23

Re: Separate menus seen as one

17 May 2021, 20:07

I realise that I can simply make the menu item names different; I was hoping that there would be a simple way to deal with the situation where separate menus contain menu items with the same name.
User avatar
boiler
Posts: 16911
Joined: 21 Dec 2014, 02:44

Re: Separate menus seen as one

17 May 2021, 20:30

You could make the key of the associative array contain a combination of the menu position and its name:

Code: Select all

; changed from reading file lines to parsing an assigned string for this demo
menuitemstext =
(
MySubmenu;Test;Result 1
;Test;Result 2
)
global AssocArray := {}
Array := []
Loop, Parse, menuitemstext, `n
    Array.Push(StrSplit(A_LoopField, ";"))
for index, element in Array {
    if (element.1 > "") {
        Menu, % element.1, Add, % element.2, Function
        Menu, MyMenu, Add, % element.1, % ":" element.1
    } else {
        Menu, MyMenu, Add, % element.2, Function
    }
    AssocArray[(element.1 ? 2 : 1) "-" element.2] := element.3
}
Menu, MyMenu, Show

Function(ItemName, ItemPos) {
    MsgBox, % AssocArray[ItemPos "-" ItemName]
    return
}
User avatar
mikeyww
Posts: 26869
Joined: 09 Sep 2014, 18:38

Re: Separate menus seen as one  Topic is solved

17 May 2021, 20:32

It was already solved in your earlier thread where an additional array dimension was used.

https://www.autohotkey.com/boards/viewtopic.php?p=399464#p399464
Ecimeric
Posts: 130
Joined: 11 Jan 2017, 02:23

Re: Separate menus seen as one

18 May 2021, 01:41

boiler wrote:
17 May 2021, 20:30
You could make the key of the associative array contain a combination of the menu position and its name:

Code: Select all

menuitems.txt
MySubmenu;Test 1;Result 1
MySubmenu;Test 2;Result 2
;Test;Result 2
Thank you; however, if I expand menuitems.txt, only Test 2 in MySubmenu returns a result.
User avatar
boiler
Posts: 16911
Joined: 21 Dec 2014, 02:44

Re: Separate menus seen as one

18 May 2021, 02:22

Yes, that will only work for the specific case where the combination of name and position would be unique (and have to be stored accordingly). The best way is what mikeyww mentioned that he already showed you, which is to base it on the menu name and item name combination.
Ecimeric
Posts: 130
Joined: 11 Jan 2017, 02:23

Re: Separate menus seen as one

18 May 2021, 03:41

I think I have it working now with

Code: Select all

AssocArray[(element.1 ? element.1 : "MyMenu"), element.2] := element.3

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], prototype_zero and 218 guests