objects: hierarchies: listing/comparing objects

Get help with using AutoHotkey and its commands and hotkeys
User avatar
jeeswg
Posts: 5152
Joined: 19 Dec 2016, 01:58
Location: UK

objects: hierarchies: listing/comparing objects

17 Dec 2017, 15:43

What are the best links currently, for listing all the keys in an object/checking if two objects match. That recurse through hierarchies. Thanks.
teadrinker
Posts: 387
Joined: 29 Mar 2015, 09:41
Contact:

Re: objects: hierarchies: listing/comparing objects

18 Dec 2017, 01:35

To compare two objects I'd transform objects to buffers and then compare buffers through memcmp.
User avatar
jeeswg
Posts: 5152
Joined: 19 Dec 2016, 01:58
Location: UK

Re: objects: hierarchies: listing/comparing objects

17 Jan 2018, 06:41

- @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?
User avatar
jeeswg
Posts: 5152
Joined: 19 Dec 2016, 01:58
Location: UK

Re: objects: hierarchies: listing/comparing objects

19 Jan 2018, 07:39

- 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)
User avatar
jeeswg
Posts: 5152
Joined: 19 Dec 2016, 01:58
Location: UK

Re: objects: hierarchies: listing/comparing objects

19 Jan 2018, 08:52

- 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
Last edited by jeeswg on 22 Jan 2018, 13:04, edited 1 time in total.
Helgef
Posts: 3198
Joined: 17 Jul 2016, 01:02
Contact:

Re: objects: hierarchies: listing/comparing objects

19 Jan 2018, 12:26

The usual comment: it doesn't handle circular references ;).
More:
User avatar
jeeswg
Posts: 5152
Joined: 19 Dec 2016, 01:58
Location: UK

Re: objects: hierarchies: listing/comparing objects

19 Jan 2018, 13:23

- 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?
Helgef
Posts: 3198
Joined: 17 Jul 2016, 01:02
Contact:

Re: objects: hierarchies: listing/comparing objects

19 Jan 2018, 13:36

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.

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], paik1002, roysubs, swagfag and 24 guests