2d array returning blank Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
User avatar
Bugz000
Posts: 29
Joined: 30 Sep 2013, 10:01

2d array returning blank

21 Apr 2018, 08:58

Code: Select all

Arr := []
Arr[2] := "R"
Arr[2, o] := A_TickCount
msgbox % Arr[2, o]
this code returns blank, could someone explain how i'm doing 2d arrays wrong? :)
msgbox % arr[2] returns "R" as expected
Image
swagfag
Posts: 1266
Joined: 11 Jan 2017, 17:59

Re: 2d array returning blank

21 Apr 2018, 09:24

from https://autohotkey.com/docs/Objects.htm ... _of_Arrays
Multi-dimensional assignments such as table[a, b, c, d] := value are handled as follows:
  • If there is only one key remaining, perform the assignment and return. Otherwise:
    Search the object for the first key in the list.
    If a non-object is found, fail.
    If an object is not found, create one and store it.
    Recursively invoke the sub-object, passing the remaining keys and value - repeat from the top
you assigned here, so the rest fails

Code: Select all

Arr[2] := "R"
either dont assign

Code: Select all

Arr := []
;Arr[2] := "R"
Arr[2, "o"] := A_TickCount
msgbox % Arr[2, "o"]
or overwrite the assignment

Code: Select all

Arr := []
Arr[2] := "R"
Arr[2] := {"o" : A_TickCount}
msgbox % Arr[2, "o"]
string keys need quotes also
User avatar
jeeswg
Posts: 5164
Joined: 19 Dec 2016, 01:58
Location: UK

Re: 2d array returning blank

21 Apr 2018, 09:57

- I have a section called: A KEY CAN HAVE A VALUE OR CHILD KEYS, BUT NOT BOTH, here:
jeeswg's objects tutorial - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=29232
- I would think that a normal person would not expect this behaviour.
- However, if obj.key could have both a string value and object subkeys, what should obj.key return? Perhaps there could be ObjGetString and ObjGetChildren, but these are just my personal ideas, I don't know if any programming language does something like that.
User avatar
Bugz000
Posts: 29
Joined: 30 Sep 2013, 10:01

Re: 2d array returning blank

21 Apr 2018, 13:43

ah thankyou for clearing that up, though i've wanted a certain functionality for a while, it's what i'm trying to closely replicate with 2d arrays but as you can see the actual functionality is quite different


box.material := "cardboard"
box.width := 100
box.height := 200
box.depth := 300
msgbox % box.material

i'm considering writing a lib to achieve this as i believe it has something to do with objects or classes? it's something which i haven't touched upon in my years of using ahk
can anyone point me toward the resources/information that i'd need to do (or closely achieve) this? :)
Last edited by Bugz000 on 21 Apr 2018, 13:46, edited 1 time in total.
Image
swagfag
Posts: 1266
Joined: 11 Jan 2017, 17:59

Re: 2d array returning blank

21 Apr 2018, 13:46

achieve what exactly, what are u trying to do in the first place
User avatar
Bugz000
Posts: 29
Joined: 30 Sep 2013, 10:01

Re: 2d array returning blank

21 Apr 2018, 13:47

it's more an issue with handling data

i wish to essentially have variables which can store more than one value
for instance
Box1.material := "cardboard"
Box2.material := "wood"
Box1.width := 200

is this possible? i'm sure i've seen it somewhere

i now understand arrays can do this

Box[1, "material"] := "Cardboard"
Box[2, "material"] := "wood"
msgbox % Box[2, "material"]

however the syntax bugs me - though if it's all that's available that is fine :)
Image
swagfag
Posts: 1266
Joined: 11 Jan 2017, 17:59

Re: 2d array returning blank

21 Apr 2018, 14:24

there is a multitude of ways how to accomplish that

Code: Select all

class Box {
	__New(material, width, height) {
		this.material := material
		this.width := width
		this.height := height
	}
}

CollectionOfBoxes := [{"material" : "cardboard", "width" : 200, "height" : 200}
					, {"material" : "wood", "width" : 100, "height" : 500}
					, {"material" : "paper", "width" : 400, "height" : 300}]

steelBox := {}
steelBox.material := "steel"
steelBox.width := 50
steelBox.height := 50
CollectionOfBoxes.push(steelBox)


plasticBox := new Box("PET", 200, 250)
CollectionOfBoxes.push(plasticBox)

for index, element in CollectionOfBoxes {
	MsgBox, % "ID: " . index 
		. "`r`n" . "Material: " . element.material
		. "`r`n" . "Width: " . element.width
		. "`r`n" . "Height: " . element.height
}


fourthBox := CollectionOfBoxes[4]
MsgBox, % "Fourth Box in the collection " 
	. "`r`n" . "Material: " . CollectionOfBoxes[4].material
	. "`r`n" . "Width: " . fourthBox.width
	. "`r`n" . "Height: " . CollectionOfBoxes[4, "height"]
User avatar
jeeswg
Posts: 5164
Joined: 19 Dec 2016, 01:58
Location: UK

Re: 2d array returning blank  Topic is solved

21 Apr 2018, 15:50

It's generally long-winded when you assign it, but simple when you retrieve it.

Code: Select all

q::
Box := []
Box[1, "material"] := "cardboard"
Box[2, "material"] := "wood"
MsgBox, % Box.2.material
return
User avatar
Bugz000
Posts: 29
Joined: 30 Sep 2013, 10:01

Re: 2d array returning blank

21 Apr 2018, 16:20

jeeswg wrote:It's generally long-winded when you assign it, but simple when you retrieve it.

Code: Select all

q::
Box := []
Box[1, "material"] := "cardboard"
Box[2, "material"] := "wood"
MsgBox, % Box.2.material
return

Aha this is where I've seen it! Thankyou, was not aware it could be retrieved this way, thank you :)
Image

Return to “Ask For Help”

Who is online

Users browsing this forum: 99muppets, Google [Bot], ryancoll22, WingbtZ and 29 guests