how to create nesting object Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
cgx5871
Posts: 319
Joined: 26 Jul 2018, 14:02

how to create nesting object

08 May 2023, 09:57

v1 code:

Code: Select all

a:={}
a["b","c","d","e"]:=3
MsgBox % a.b.c.d.e
v2 how to create this?
User avatar
mikeyww
Posts: 27366
Joined: 09 Sep 2014, 18:38

Re: how to create nesting object

08 May 2023, 10:05

There may be better ways.

Code: Select all

#Requires AutoHotkey v2.0
a := {}
a.b := {}
a.b.c := {}
a.b.c.d := {}
a.b.c.d.e := 3
MsgBox a.b.c.d.e
I think I saw a script a while back that was written to make this easier or more straightforward.

AHK v2 uses expressions.
User avatar
FanaticGuru
Posts: 1908
Joined: 30 Sep 2013, 22:25

Re: how to create nesting object  Topic is solved

08 May 2023, 14:29

cgx5871 wrote:
08 May 2023, 09:57
v1 code:

Code: Select all

a:={}
a["b","c","d","e"]:=3
MsgBox % a.b.c.d.e
v2 how to create this?

In v2, you are mixing two things:
a:={} <-- this is object syntax
a["b","c","d","e"]:=3<-- this is map syntax (multi-dimensional map)
MsgBox % a.b.c.d.e <-- this is object syntax

If you want to use an object with properties, you can make that a little easier with this function as @mikeyww suggested so you don't have to predefine default objects:

Code: Select all

ObjectDefaultCreate()
ObjectDefaultCreate() => Object.Prototype.DefineProp('__Get', {Call: (this, Name, Params) => this.%Name% := {}})

a:={}
a.b.c.d.e := 3
Msgbox a.b.c.d.e

Multi-dimension maps are not natively supported in v2. If you want to do a multi-dimension map, this function will allow that:

Code: Select all

a := MapN()
a["b", "c", "d", "e"] := 3
MsgBox a["b", "c", "d", "e"]

; Helgef: An "n-dimensional" map.
class MapN extends map {
	static __new()
		=> this.prototype.class := this
	__item[k1, p*] {
		get => p.length	? super[k1][p*] : super[k1]
		set {
			if	p.length && (!super.has(k1) || !(super[k1] is this.class))
				super[k1] := (this.class)()
			( p.length ) ? super[k1][p*] := value : super[k1] := value
		}
	}
}

So you can do either pretty much like in v2 but you cannot easily mix a.b.c.d.e syntax with a["b", "c", "d", "e"] as these are two different type object syntaxes.

In v1 there was one generic object, in v2 there are 3 different types of objects primarily for data:
  • Object (the base generic object which uses Properties, Methods, and Values)
  • Array (an extension of base object that uses a numbered Index and Values)
  • Map (an extension of base object that uses mapped pairs of string Keys and Values)

There are other object types like Gui but Object, Array, and Map are the three main ones used for data. I would look each of those up in the documents to see the difference. They each have specific syntax.

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
cgx5871
Posts: 319
Joined: 26 Jul 2018, 14:02

Re: how to create nesting object

10 May 2023, 12:13

"So you can do either pretty much like in v2 but you cannot easily mix a.b.c.d.e syntax with a["b", "c", "d", "e"] as these are two different type object syntaxes."
=========================================

so Pity
I always think this is the biggest charm of v1

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: arout and 79 guests