Shortest way to define a 2D Map

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
DrReflex
Posts: 42
Joined: 25 May 2015, 02:57
Location: Greer, SC

Shortest way to define a 2D Map

22 Jan 2024, 03:19

I can define a 2D object as:

oGrid := {1:{1:1,2:2,3:3},
2:{1:4,2:5,3:6},
3:{1:7,2:8,3:9}}


and, I can define a 2D array as:

aGrid := [[1,2,3],
[4,5,6],
[7,8,9]]


Is there a shorter way to define a 2D map than:

mGrid := Map(1,Map(1,1, 2,2, 3,3),
2,Map(1,4, 2,5, 3,6),
3,Map(1,7, 2,8, 3,9))


Lexios and other posts in "Map shorthand" viewtopic.php?f=37&t=67213&p=299127&hilit=map+definition#p299127 suggests that there may not be a shorter way to define the 2D Map mGrid.
User avatar
mikeyww
Posts: 26996
Joined: 09 Sep 2014, 18:38

Re: Shortest way to define a 2D Map

22 Jan 2024, 06:04

If your keys are really 1, 2, and 3, then it can be an array if you want one.

Code: Select all

#Requires AutoHotkey v2.0
mGrid := [Map(1, 2), Map(3, 4), Map(5, 6)]
MsgBox mGrid[2][3]
I'm not sure what you mean by "shorter", as I believe that defining a map always requires using the term "Map".
User avatar
DrReflex
Posts: 42
Joined: 25 May 2015, 02:57
Location: Greer, SC

Re: Shortest way to define a 2D Map

24 Jan 2024, 02:33

Thanks Mike,

I should have labeled the post "Shortest way to define a multidimensional map and should have left the examples as 3x3x3 3D.

It looks like your example is an array of maps.

I am looking for a shorthand way to write a multidimensional map of maps.

To keep things simple and similar between the examples, I cut the examples from 3 dimensions of 3x3x3 to 2 dimensions of 3x3. I also converted the map keys to integers. This worked well for visual comparison between the three examples by eliminating the introduction a lot of quotes. The quotes tended to hide the issue of the nested Map() statements (due to the apparent lack of a shorthand method for defining maps).

In my examples, the definitions for the multidimensional object and for the multidimensional array can be written in AHK shorthand notation (see above). This allows one to avoid using Object() and Array() at every dimensional branch in the definitions respectively. NOTE TO OTHERS: Object() and Array() are short for Object.Call() and Array.Call() respectively.

I cannot find a similar AHK shorthand notation to use to define a multidimensional map. This forces me to insert a "Map()" short for Map.Call() at every dimensional branch in the definition.

I was wondering if there might be an AHK shorthand similar to that available for objects and for arrays that works for maps. WHY? Remember these are simple 3x3 2D examples that I cut back from 3x3x3 3D examples. I am working with some VERY LARGE multidimensional associative matrices. The number of Map() statements approaches exponential expansion as the number of dimensions and number of entries per dimension are increased.

A shorthand, something like the following, would be nice!

mGrid := [1:[1:1, 2:2, 3:3],
2:[1:4, 2:5, 3:6],
3:[1:7, 2:8, 3:9]]
User avatar
Seven0528
Posts: 346
Joined: 23 Jan 2023, 04:52
Location: South Korea
Contact:

Re: Shortest way to define a 2D Map

24 Jan 2024, 06:50

 If you were to employ RegExMatch, well, it might not be impossible, but...
  • English is not my native language. Please forgive any awkward expressions.
  • 영어는 제 모국어가 아닙니다. 어색한 표현이 있어도 양해해 주세요.
User avatar
mikeyww
Posts: 26996
Joined: 09 Sep 2014, 18:38

Re: Shortest way to define a 2D Map

24 Jan 2024, 07:03

Already noted, but as far as I know, defining a map always requires using the term "Map".
User avatar
Seven0528
Posts: 346
Joined: 23 Jan 2023, 04:52
Location: South Korea
Contact:

Re: Shortest way to define a 2D Map

24 Jan 2024, 07:44

 Yes, I agree with what you're saying...
I have considered automatically creating a Map through a string.
  • English is not my native language. Please forgive any awkward expressions.
  • 영어는 제 모국어가 아닙니다. 어색한 표현이 있어도 양해해 주세요.
neogna2
Posts: 591
Joined: 15 Sep 2016, 15:44

Re: Shortest way to define a 2D Map

24 Jan 2024, 07:48

You can use a shorter alias for the Map class

Code: Select all

class _ extends Map {
}
mGrid := _(1,_(1,1, 2,2, 3,3),
2,_(1,4, 2,5, 3,6),
3,_(1,7, 2,8, 3,9))
MsgBox mGrid[2][3] ;6
I don't know any built in way shorter than that. One reason lexikos hasn't added one may be that the characters [ and { and ( and < already have other meanings when standalone and there's no other bracket-like character that is easy to type on a US keyboard.

For your own personal code if you're hand typing the multidimensional maps you could invent some shorthand syntax and then later select the text in your editor and use a second script to expand from shorthand to valid Map() before you save and run the script.
User avatar
rommmcek
Posts: 1478
Joined: 15 Aug 2014, 15:18

Re: Shortest way to define a 2D Map

24 Jan 2024, 16:15

Maybe:

Code: Select all

#Requires AutoHotkey v2+
#SingleInstance Force

Loop (mGrid:= Map(), aInd:=aVal:=0, 10)
    loop (aInd:=A_Index, mGrid.Has(A_Index)? "": mGrid[aInd]:= Map(), 5)
        mGrid[aInd][A_Index]:= ++aVal
            
for i, j in (txt:="", mGrid)
    for k, v in j
        ;txt.= i ":[" k ":" v "]`n" 
        txt.= (Mod(i,mGrid.Count)=1&&Mod(k,j.Count)=1? "[" i ":": Mod(k,j.Count)=1? i ":": "")
           .  (k=1? "[": "") k ":" v (mod(k,j.Count)? ", ": "")
           .  (!Mod(k,j.Count)&&Mod(i,mGrid.Count)? "],`n": "") (!Mod(k,j.Count)&&!Mod(i,mGrid.Count)? "]]": "")
MsgBox txt
Last edited by rommmcek on 28 Jan 2024, 05:49, edited 3 times in total.
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: Shortest way to define a 2D Map

24 Jan 2024, 17:22

I use this Prototype code to make working with multidimension maps much easier.

Code: Select all

; Helgef: An "n-dimensional" map; ntepa: Map.Prototype Class; FanaticGuru: fat arrow Function
; https://www.autohotkey.com/boards/viewtopic.php?p=520860#p520860
MapN() => (__set := Map.Prototype.GetOwnPropDesc("__Item").set, __get := Map.Prototype.GetOwnPropDesc("__Item").get, Map.Prototype.DefineProp("__Item", { get: (self, k1, p*) => p.Length ? __get(self, k1)[p*] : __get(self, k1), set: (self, value, k1, p*) => (p.Length && (!(Map.Prototype.Has)(self, k1) || !(__get(self, k1) is Map)) && __set(self, (m := Map(), m.CaseSense := self.CaseSense, m), k1), p.Length ? __get(self, k1)[p*] := value : __set(self, value, k1)) }))
MapN ; Initialize

m := Map()
m[1, 2, 3] := 123
m["a", "b", "c"] := "abc"
m["r", "s", "t", "u", "v", "w", "x", "y", "z"] := 'nine dimensions on the fly'
MsgBox m[1, 2, 3]
MsgBox m["a", "b", "c"]
MsgBox m["r", "s", "t", "u", "v", "w", "x", "y", "z"]
After you execute MapN to modify the prototype of Map then multidimension gets much easier to work with.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: alawsareps, Bing [Bot], Google [Bot], RussF and 76 guests