Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

[function] Explore object


  • Please log in to reply
3 replies to this topic
Learning one
  • Members
  • 1483 posts
  • Last active: Jan 02 2016 02:30 PM
  • Joined: 04 Apr 2009

WARNING: This is old thread. It is continued here.

:arrow: Edit: see 2. or 3. post

Original post:
It's similar to ListVars, but for objects; displays key/value pairs, hierarchy, sub-objects and their key/value pairs...
Explores up to 12 levels deep. Still not polished. (And yes, function looks crazy. happy.png)
- Related: List Arrays/Objects easily (GUI) %5BLib%5D %5BAHK_L%5D by shajul
;====== Testing area ======
oKey3 := Object("Key3.1", "value 3.1", "Key3.2", "value 3.2")
oKey4 := Object("Key4.1", "value 4.1", "Key4.2", Object("Key4.2.1", "value 4.2.1", "Key4.2.2", "value 4.2.2"), "Key4.3", "value 4.3")
oKey5 := Object("Key5.1", "value 5.1", "Key5.2", "value 5.2", "Key5.3", "value 5.3")

oRoot := Object()
oRoot.Key1 := "value 1"
oRoot.Key2 := "value 2"
oRoot.Key3 := oKey3
oRoot.Key4 := oKey4
oRoot.Key5 := oKey5

MsgBox,, Exploring Root object, % ExploreObj(oRoot)
MsgBox,, Exploring Key4 object, % ExploreObj(oKey4)


;====== Function ======
ExploreObj(Obj, NewRow="`n", Equal=" = ", ind = "`t") {
v0 := Obj
For k1,v1 in v0
{
If (IsObject(v1))
{
ToReturn .= k1 NewRow
For k2,v2 in v1
{
If (IsObject(v2))
{
ToReturn .= ind k2 NewRow
For k3,v3 in v2
{
If (IsObject(v3))
{
ToReturn .= ind ind k3 NewRow
For k4,v4 in v3
{
If (IsObject(v4))
{
ToReturn .= ind ind ind k4 NewRow
For k5,v5 in v4
{
If (IsObject(v5))
{
ToReturn .= ind ind ind ind k5 NewRow
For k6,v6 in v5
{
If (IsObject(v6))
{
ToReturn .= ind ind ind ind ind k6 NewRow
For k7,v7 in v6
{
If (IsObject(v7))
{
ToReturn .= ind ind ind ind ind ind k7 NewRow
For k8,v8 in v7
{
If (IsObject(v8))
{
ToReturn .= ind ind ind ind ind ind ind k8 NewRow
For k9,v9 in v8
{
If (IsObject(v9))
{
ToReturn .= ind ind ind ind ind ind ind ind k9 NewRow
For k10,v10 in v9
{
If (IsObject(v10))
{
ToReturn .= ind ind ind ind ind ind ind ind ind k10 NewRow
For k11,v11 in v10
{
If (IsObject(v11))
{
ToReturn .= ind ind ind ind ind ind ind ind ind ind k11 NewRow
For k12,v12 in v11
ToReturn .= ind ind ind ind ind ind ind ind ind ind ind k12 Equal v12 NewRow
}
else
ToReturn .= ind ind ind ind ind ind ind ind ind ind k11 Equal v11 NewRow
}
}
else
ToReturn .= ind ind ind ind ind ind ind ind ind k10 Equal v10 NewRow
}
}
else
ToReturn .= ind ind ind ind ind ind ind ind k9 Equal v9 NewRow
}
}
else
ToReturn .= ind ind ind ind ind ind ind k8 Equal v8 NewRow
}
}
else
ToReturn .= ind ind ind ind ind ind k7 Equal v7 NewRow
}
}
else
ToReturn .= ind ind ind ind ind k6 Equal v6 NewRow
}
}
else
ToReturn .= ind ind ind ind k5 Equal v5 NewRow
}
}
else
ToReturn .= ind ind ind k4 Equal v4 NewRow
}
}
else
ToReturn .= ind ind k3 Equal v3 NewRow
}
}
else
ToReturn .= ind k2 Equal v2 NewRow
}
}
else
ToReturn .= k1 Equal v1 NewRow
}
Return RTrim(ToReturn, NewRow)
}


Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006
If you write it as a recursive function, it's a lot shorter and also supports an arbitrary depth.
ExploreObj(Obj, NewRow="`n", Equal="  =  ", Indent="`t", Depth=12, CurIndent="") {
    for k,v in Obj
        ToReturn .= CurIndent . k . [color=brown](IsObject(v) && depth>1 ?[/color] NewRow . ExploreObj(v, NewRow, Equal, Indent, Depth-1, CurIndent . Indent) [color=brown]:[/color] Equal . v[color=brown])[/color] . NewRow
    return RTrim(ToReturn, NewRow)
}


Learning one
  • Members
  • 1483 posts
  • Last active: Jan 02 2016 02:30 PM
  • Joined: 04 Apr 2009
That's much better. Bravo! Thanks! :p

Here is a version that removes all new lines, carriage returns and by default, displays only first 60 characters - like ListVars. Applies on both keys and values. And Depth is second param now.
ExploreObj(Obj, Depth=12, NewRow="`n", Equal="  =  ", Indent="`t", CurIndent="") {
	; Link:	www.autohotkey.com/forum/topic69253.html
	static ShowChar := 60	; number of characters to show
	For k,v in Obj {
		StringReplace, k, k, `n,,all
		StringReplace, k, k, `r,,all
		k := (StrLen(k) > ShowChar) ? SubStr(k,1,ShowChar) " ..." : k
		if (IsObject(v))
		ToReturn .= CurIndent . k . NewRow . (depth>1 ? %A_ThisFunc%(v, Depth-1, NewRow, Equal, Indent, CurIndent . Indent) . NewRow : "")
		else {
			StringReplace, v, v, `n,,all
			StringReplace, v, v, `r,,all
			v := (StrLen(v) > ShowChar) ? SubStr(v,1,ShowChar) " ..." : v, ToReturn .= CurIndent . k . Equal . v . NewRow
		}
    }
	return RTrim(ToReturn, NewRow)
}


;====== Testing area ======
oKey3 := Object("Key`n`n3.1", "value `n`n3.1", "Key3.2", "value 3.2")
oKey4 := Object("Key4.1", "value 4.1",  "Key4.2", Object("Key4.2.1", "value 4.2.1", "Key4.2.2", "value 4.2.2"), "Key4.3", "value 4.3")
oKey5 := Object("Key5.1", "value 5.1", "Key5.2", "value 5.2", "Key5.3", "value 5.3")

oRoot := Object()
oRoot["Key`n`n1"] := "value 1 `n`nThe string to search for. Matching is not case sensitive unless StringCaseSense has been turned on."
oRoot.Key2 := "value 2"
oRoot.Key3 := oKey3
oRoot.Key4 := oKey4
oRoot.Key5 := oKey5

MsgBox,, Exploring Root object, % ExploreObj(oRoot)
MsgBox,, Exploring Root object - 1 level deep, % ExploreObj(oRoot,1)
MsgBox,, Exploring Key4 object, % ExploreObj(oKey4)
MsgBox,, Exploring oRoot.Key4["Key4.2"] object, % ExploreObj(oRoot.Key4["Key4.2"])


JovanniB
  • Members
  • 25 posts
  • Last active: Aug 21 2015 02:06 PM
  • Joined: 31 Jul 2013

Hi,

 

I've the problem (1.1.15.00 Ansi 32 bit).

 

a) using exploreobj in Script X with 2-dim-Array-variables, everything is fine, output (via fileappend for debugging resaons) is as it should be

 

and

 

B) using exploreobj in Script Y with 2-dim-Array-variables (at the same computer, using notepad++ to look into the generated file) no correct crlf and no correct tabs - it prints "`n" and "`t" despite a correct crlf and despite a corret tab.

 

How about that ??