Page 1 of 1

objects: hierarchies: listing/comparing objects

Posted: 17 Dec 2017, 15:43
by jeeswg
What are the best links currently, for listing all the keys in an object/checking if two objects match. That recurse through hierarchies. Thanks.

Re: objects: hierarchies: listing/comparing objects

Posted: 18 Dec 2017, 01:35
by teadrinker
To compare two objects I'd transform objects to buffers and then compare buffers through memcmp.

Re: objects: hierarchies: listing/comparing objects

Posted: 17 Jan 2018, 06:41
by jeeswg
- @teadrinker: Great link, many thinks.
- Any other simple scripts to just list in text form, all of the keys and values in an object, indented?

Re: objects: hierarchies: listing/comparing objects

Posted: 19 Jan 2018, 07:39
by jeeswg
- Anyone? I'd be interested in multiple scripts for listing object keys/values, for the following reasons.
- A lot of the more complicated scripts I use involve hierarchies, and I'm wanting to try and streamline all of my hierarchy/recursion/family tree/ancestry scripts to work within one model, and to be as efficient as possible.
- Also, when I recently did my Acc script to retrieve text from all GUI elements, I intended to base it on another script, but because of differences in how things are structured, I had to rewrite the script completely.
- E.g. areas:
registry keys/values
files/folders
Acc GUI elements
treeview controls
JSON
object (array) keys/values
webpage elements
window controls
(ini files)

Re: objects: hierarchies: listing/comparing objects

Posted: 19 Jan 2018, 08:52
by jeeswg
- Well, I had a go at a function, it seems to be working pretty well.
- Many thanks to GeekDude, for his 'array defined like JSON text' example, that I was lucky enough to come across the other day, which I used as the basis for the example below, used for testing the function. (I removed the OTB style of bracing, added indentation and rearranged everything to be in alphabetical order to match the function's output.)
- I haven't done too many recursive functions before, a bit of a first.

Code: Select all

q:: ;object - list keys/values
;object example based on:
;GeekDude's tips and tricks - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=7&t=7190

;note: to make this work in AHK v2
;replace: (LTrim Join
;with: (LTrim Join Q

;http://www.json.org/example.html
oArray :=
(LTrim Join
{
	"glossary":
	{
		"GlossDiv":
		{
			"GlossList":
			{
				"GlossEntry":
				{
					"Abbrev": "ISO 8879:1986",
					"Acronym": "SGML",
					"GlossDef":
					{
						"GlossSeeAlso":
						[
							"GML",
							"XML"
						],
						"para": "A meta-markup language, used to create markup languages such as DocBook."
					},
					"GlossSee": "markup",
					"GlossTerm": "Standard Generalized Markup Language",
					"ID": "SGML",
					"SortAs": "SGML"
				}
			},
			"title": "S"
		},
		"title": "example glossary"
	}
}
)

MsgBox, % Clipboard := JEE_ObjList(oArray)

oArray := ["a", "b", "c"]

MsgBox, % Clipboard := JEE_ObjList(oArray)
return

JEE_ObjList(oArray, vSep:=" ", vRecurse:=1)
{
	if !vRecurse
	{
		for vKey, vValue in oArray
			vOutput .= vKey vSep vValue "`r`n"
		return SubStr(vOutput, 1, -2)
	}
	for vKey, vValue in oArray
	{
		;note: vRecurse is used to indicate depth, i.e. how many tabs to use
		if IsObject(vValue)
			vOutput .= JEE_StrRept("`t", vRecurse-1) vKey vSep "OBJECT`r`n" %A_ThisFunc%(vValue, vSep, vRecurse+1) "`r`n"
		else
			vOutput .= JEE_StrRept("`t", vRecurse-1) vKey vSep vValue "`r`n"
	}
	return SubStr(vOutput, 1, -2)
}

JEE_StrRept(vText, vNum)
{
	if (vNum <= 0)
		return
	return StrReplace(Format("{:" vNum "}", ""), " ", vText)
	;return StrReplace(Format("{:0" vNum "}", 0), 0, vText)
}
- [EDIT:][examples: define an array JSON-style]
GeekDude's tips and tricks - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=7190
Object declaration syntax - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 37&t=43154

Re: objects: hierarchies: listing/comparing objects

Posted: 19 Jan 2018, 12:26
by Helgef
The usual comment: it doesn't handle circular references ;).
More:

Re: objects: hierarchies: listing/comparing objects

Posted: 19 Jan 2018, 13:23
by jeeswg
- Many thanks for the links Helgef. I'd probably have to simplify the 3 scripts to get any code that lists the keys/values. More stuff for the to-do list, it's so long now.
- Re. circular references, I asked about them on the other thread, any good threads relating to them? I've come across almost every object issue imaginable while going through links, but not that one curiously. To be honest though, object classes, and bugs, the number is gigantic. A difficult question, but, any key bug issues to be aware of, that I haven't already mentioned, I'm probably not aware of them, so you're welcome to expand my object bug horizons at any point.
- Btw I don't put circular references into my scripts hehe, so how am I supposed to help users who do?

Re: objects: hierarchies: listing/comparing objects

Posted: 19 Jan 2018, 13:36
by Helgef
how am I supposed to help users who do?
You can specify a max recursion depth. Also, you can look how HotKeyIt does in the link teadrinker (hello :wave: ) provided.

Cheers.