Array v1 to v2 Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
TheDewd
Posts: 1513
Joined: 19 Dec 2013, 11:16
Location: USA

Array v1 to v2

27 Mar 2024, 09:32

Edit: Going to use Map(). Seems to do what I need.

So, I'm finally trying to learn the v2 syntax by converting one of my v1 scripts.

Below is an example of an array, the way I've formatted it in v1:

Code: Select all

MyArray := []
MyArray[1] := "MyText1"
MyArray[2] := "MyText2"
MyArray[3] := "MyText3"
This code is giving me an error in v2, so I would assume this has changed.

If I require a specific value to be assigned to a specific index within the array, what is the new way to achieve this?

I think the code I have below will work -- but I would like to know if this is the recommended way going forward, or are there still alternative ways to do this like the code above?

Code: Select all

MyArray := []
MyArray.InsertAt(1, "MyText1")
MyArray.InsertAt(2, "MyText2")
MyArray.InsertAt(3, "MyText3")
Or maybe this is the new way?

Code: Select all

MyArr := Map()
MyArr[1] := "MyText1"
MyArr[2] := "MyText2"
MyArr[3] := "MyText3"
Last edited by TheDewd on 27 Mar 2024, 10:41, edited 1 time in total.
vmech
Posts: 356
Joined: 25 Aug 2019, 13:03

Re: Array v1 to v2  Topic is solved

27 Mar 2024, 09:56

@TheDewd

Code: Select all

MyArray := ["MyText1" , "MyText2" , "MyText3"]
MyMap := Map("1" , "MyText1" , "2" , "MyText2" , "3" , "MyText3")
Also multiline definitions allowed:

Code: Select all

MyArray := [
  "MyText1",
  "MyText2",
  "MyText3"
]
MyMap := Map(
  "1" , "MyText1" ,
  "2" , "MyText2" ,
  "3" , "MyText3"
)
Last edited by vmech on 27 Mar 2024, 10:01, edited 1 time in total.
Please post your script code inside [code] ... [/code] block. Thank you.
Rohwedder
Posts: 7655
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Array v1 to v2

27 Mar 2024, 09:57

Hallo,
the v2 arrays are too cumbersome for me. I was happy when I found HelgeffegleH's Class Map_n:

Code: Select all

#Requires AutoHotkey v2.0
MyMap := Map_n()
MyMap[1] := "MyText1"
MyMap[2] := "MyText2"
MyMap[3] := "MyText3"
MsgBox MyMap[1] " > " MyMap[2] " > " MyMap[3]

mn := Map_n() ; Create a new maps_n
; Store some values:
mn[1, 1, 1] := 'a'			
mn[1, 1, 2] := 'b'
mn['x', 'y', 'z'] := 1
mn['x', 'y', 'w'] := 2
; Note that you can store values at any "depth", but this loop only works for 3 indices.
; Print:
for index_1, sub_map1 in mn
	for index_2, sub_map2 in sub_map1
		for index_3, value in sub_map2
			msgbox  'mn( ' . index_1 .  ', ' . index_2 . ', ' . index_3 . ' ) := ' . value
; -------------------------------------------------------------
Class Map_n extends Map { ; HelgeffegleH: "n-dimensional" Map
	Static __new() => this.Prototype.Class := this ; MyMap := Map_n()
	__item[K, P*] {
	get {
		Return P.Length?super[K][P*]:super[K]
	} set {
	If	P.Length&&(!super.has(K)||!(super[K] is this.Class))
		super[K] := (this.Class)()
	(P.Length)?super[K][P*] := Value:super[K] := Value
}}} ; https://github.com/HelgeffegleH/AHK-misc./tree/master/classes/map_n
Rohwedder
Posts: 7655
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Array v1 to v2

27 Mar 2024, 12:38

@vmech
What is the v2 analog to:

Code: Select all

#Requires AutoHotkey v1.1.33
Coords := {}
CoordMode, Mouse, Screen
RWin & LButton:: ; Click: Coordinate recording
	MouseGetPos, x, y	
	Coords.Push({"x":x,"y":y})
	KeyWait, LButton
Return                  
q:: ; Output of coordinates
	Text =
	For all, Coord in Coords
		Text .= Coord.x " > " Coord.y "`n"
	MsgBox,% Text
Return
Replace the ???:

Code: Select all

#Requires AutoHotkey v2.0
Coords := Map()
CoordMode "Mouse", "Screen"
RWin & LButton:: { ; Coordinate recording
	Global Coords
	MouseGetPos &x, &y
	???
	KeyWait "LButton"
}                  
q:: { ; Output of coordinates
	Global Coords
	Text := ""
	For all, Coord in Coords
		Text .= ??? "`n"
	MsgBox Text
}
ntepa
Posts: 429
Joined: 19 Oct 2022, 20:52

Re: Array v1 to v2

27 Mar 2024, 14:20

@Rohwedder

Code: Select all

#Requires AutoHotkey v2.0
Coords := Map()
CoordMode "Mouse", "Screen"
RWin & LButton:: { ; Coordinate recording
	Global Coords
	MouseGetPos &x, &y
	Coords.Set(Coords.Count+1, {x:x, y:y})
	KeyWait "LButton"
}
q:: { ; Output of coordinates
	Global Coords
	Text := ""
	For all, Coord in Coords
		Text .= Coord.x ">" Coord.y "`n"
	MsgBox Text
}
Rohwedder
Posts: 7655
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Array v1 to v2

27 Mar 2024, 15:35

Thank you very much!
That really annoyed me. Like with a rental car. You know there's a reverse gear, just how do you put it in?
vmech
Posts: 356
Joined: 25 Aug 2019, 13:03

Re: Array v1 to v2

27 Mar 2024, 16:51

@Rohwedder
A much cleaner conversion would be use regular Array:

Code: Select all

#Requires AutoHotkey v2.0
Coords := []
CoordMode("Mouse", "Screen")
RWin & LButton::
{ ; Coordinate recording
	Global Coords
	MouseGetPos(&x, &y)
	Coords.Push({x:x, y:y})
	KeyWait("LButton")
}
q::
{ ; Output of coordinates
	Global Coords
	Text := ""
	For Coord In Coords
		Text .= Coord.x ">" Coord.y "`n"
	MsgBox(Text)
}
Please post your script code inside [code] ... [/code] block. Thank you.
Rohwedder
Posts: 7655
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Array v1 to v2

28 Mar 2024, 04:26

Very good, just like with the good old v1.1!
After my first v2 attempts (I still hate the UnsetError!), I had deemed the v2 Arrays useless and only used the obviously better Map()s and Map_n()s
I was probably a bit hasty.

But why runs both in v1 and v2:
For all, Coord in Coords but:
For Coord in Coords only in v2?

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: Dav22, Draken, loek6000, vmech and 106 guests