Displayig different-level elements of an object

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Benny-D
Posts: 302
Joined: 12 Mar 2014, 10:09

Displayig different-level elements of an object

23 Apr 2014, 07:58

I want to make a function that would display elements in an object, in which
elements are residing on different levels, that is, their addresses are of different
lengths (depths). Sorry, I don't know how to express this in words correctly, but, please,
take a look at my code, and, I think, everything will be clear:

Code: Select all

collection := {}
collection.1 := {}
collection.1.0 := "hththt"
collection.1.1 := "dfg"
collection.1.2 := "fgh"
collection.1.3 := "rhtht"
collection.2 := {}
collection.2.0 := "ert"
collection.2.1 := {}
collection.2.1.0 := "erty"
collection.2.1.1 := "ujujuju"
collection.2.1.2 := "Sandy"
collection.6 := "Merry"
collection.7 := {}
collection.7.1 := {}
collection.7.1.9 := {}
collection.7.1.9.5 := {}
collection.7.1.9.5.4 := {}
collection.7.1.9.5.4.3 := {}
collection.7.1.9.5.4.3.2 := "Linda"

address := "6"
ShowIt(collection, address) ; want to show "Merry"  
address := "2,1,2"
ShowIt(collection, address) ; want to show "Sandy"
address := "7,1,9,5,4,3,2"
ShowIt(collection, address) ; want to show "Linda"

ShowIt(ByRef Object, address)
{
msgbox % object[address]
}
  
User avatar
LinearSpoon
Posts: 156
Joined: 29 Sep 2013, 22:55

Re: Displayig different-level elements of an object

23 Apr 2014, 08:11

Commas in a variable are not considered parameter separators. You'll have to parse the address list yourself.

Code: Select all

ShowIt(Object, address)
{
  t := object
  Loop, parse, address, `,
   t := t[A_LoopField]
  msgbox % t
}
User avatar
uname
Posts: 23
Joined: 25 Oct 2013, 12:50

Re: Displayig different-level elements of an object

23 Apr 2014, 08:38

Code: Select all

collection := {}
collection[1, 0] := "hththt"
collection[1, 1] := "dfg"
collection[1, 2] := "fgh"
collection[1, 3] := "rhtht"
collection[2, 0] := "ert"
collection[2, 1, 0] := "erty"
collection[2, 1, 1] := "ujujuju"
collection[2, 1, 2] := "Sandy"
collection[6] := "Merry"
collection[7, 1, 9, 5, 4, 3, 2] := "Linda"

address := [6]
ShowIt(collection, address) ; want to show "Merry"
address := [2,1,2]
ShowIt(collection, address) ; want to show "Sandy"
address := [7,1,9,5,4,3,2]
ShowIt(collection, address) ; want to show "Linda"

ShowIt(object, address)
{
msgbox % object[address*]
}
User avatar
Benny-D
Posts: 302
Joined: 12 Mar 2014, 10:09

Re: Displayig different-level elements of an object

23 Apr 2014, 08:42

My! Just mind blowing! Thanks!
elmo
Posts: 113
Joined: 09 Oct 2013, 09:08

Re: Displayig different-level elements of an object

23 Apr 2014, 09:38

Agreed. Each more intruiging than the last.

LinearSpoon's recursion makes good sense.

The simple inclusion of an asterisk "*" in the uname's example is curious.

uname; may we trouble you for an explanation of the asterisk's role as it seems not to work when absent ?

Thanks.
Coco
Posts: 771
Joined: 29 Sep 2013, 20:37
Contact:

Re: Displayig different-level elements of an object

23 Apr 2014, 10:03

@elmo: regarding the *
See Variadic Function(s)/Variadic Function calls
This syntax can also be used when calling methods or retrieving properties of objects; for example, Object.Property[Params*]. In v1.1.12+, it can also be used for setting properties.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: OrangeCat and 246 guests