objects: get dimensions

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

objects: get dimensions

21 Feb 2018, 15:25

- I wondered if anyone had written a function to get the number of dimensions for an object.
- Prompted by this link:
Identify different types of associative arrays - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 62#p201862
- Btw for SafeArrays:
ComObjArray()
https://autohotkey.com/docs/commands/ComObjArray.htm

Code: Select all

; Get the number of dimensions:
dim := DllCall("oleaut32\SafeArrayGetDim", "ptr", ComObjValue(arr))
- Here's an attempt. [] and ["a"] are considered one-dimensional by this definition. [EDIT: The function has been corrected.]

Code: Select all

q:: ;object get dimensions
oArray1 := []
oArray2 := [[]]
oArray3 := [[[]]]
oArray4 := [[[[]]]]
MsgBox, % ObjGetDim(oArray1)
MsgBox, % ObjGetDim(oArray2)
MsgBox, % ObjGetDim(oArray3)
MsgBox, % ObjGetDim(oArray4)
return

ObjGetDim(ByRef oArray)
{
	if !IsObject(oArray)
		return 0
	oTemp := {(&oArray):1}
	vDimMax := 1
	Loop
	{
		if !oTemp.Length()
			break
		pArray := oTemp.Length()
		vDim := oTemp.Pop()
		if (vDim > vDimMax)
			vDimMax := vDim
		oArray2 := Object(pArray)
		for vKey, vValue in oArray2
			if IsObject(vValue)
				oTemp[&vValue] := vDim+1
	}
	return vDimMax
}
- Btw one question is: does an empty array have 1 dimension or 0 dimensions, my function reports such an array as having 1 dimension.
Last edited by jeeswg on 21 Feb 2018, 20:47, edited 2 times in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
teadrinker
Posts: 4325
Joined: 29 Mar 2015, 09:41
Contact:

Re: objects: get dimensions

21 Feb 2018, 17:37

H-m-m, how would you define the dimention of such array?

Code: Select all

arr := [ "", [], "", [[]], "", [[[]]] ]
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: objects: get dimensions

21 Feb 2018, 20:44

So far I have it that [] and ["a"] have the same dimension, i.e. the same capacity, even though it isn't filled, e.g. an empty folder. So by that definition, for your example you have a maximum of 4 square brackets [[[[, and so the dimensions would be 4. But I haven't seen any dimension definitions to compare it with.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
teadrinker
Posts: 4325
Joined: 29 Mar 2015, 09:41
Contact:

Re: objects: get dimensions

21 Feb 2018, 23:05

jeeswg wrote:But I haven't seen any dimension definitions to compare it with.
That's what I meant. It likely is "nested level".

Code: Select all

arr := [ "", [], "", [[]], "", [[[]]] ]
MsgBox, % ObjGetDim(arr)

ObjGetDim(arr)  {
   if !IsObject(arr)
      Return
   
   level := 0
   for k, v in arr  {
      if IsObject(v)  {
         nestLevel := ObjGetDim(v)
         ( nestLevel > level && level := nestLevel )
      }
   }
   Return ++level
}
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: objects: get dimensions

22 Feb 2018, 04:09

I think this might be pointless after all if you look into graphs there structure is more important
Also you don't check for circular references.
Recommends AHK Studio
Albireo
Posts: 1748
Joined: 16 Oct 2013, 13:53

Re: objects: get dimensions

26 Feb 2018, 09:26

Thanks!

But how to translate "&" in the instruction .:

Code: Select all

...
oTemp := {(&oArray):1}
...
?

and is it difficult to get the "deep" (how many KEYs) in each associative array in the same time?

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Giresharu and 271 guests