Page 1 of 1

2d array returning blank

Posted: 21 Apr 2018, 08:58
by Bugz000

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

Re: 2d array returning blank

Posted: 21 Apr 2018, 09:24
by swagfag
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

Re: 2d array returning blank

Posted: 21 Apr 2018, 09:57
by jeeswg
- 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.

Re: 2d array returning blank

Posted: 21 Apr 2018, 13:43
by Bugz000
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? :)

Re: 2d array returning blank

Posted: 21 Apr 2018, 13:46
by swagfag
achieve what exactly, what are u trying to do in the first place

Re: 2d array returning blank

Posted: 21 Apr 2018, 13:47
by Bugz000
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 :)

Re: 2d array returning blank

Posted: 21 Apr 2018, 14:24
by swagfag
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"]

Re: 2d array returning blank  Topic is solved

Posted: 21 Apr 2018, 15:50
by jeeswg
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

Re: 2d array returning blank

Posted: 21 Apr 2018, 16:20
by Bugz000
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 :)