PARAMETERS
- My current preferred solution based on earlier proposals:
0 = bool [default]
1 = first key name as an array
n = first n key names as an array
-1 = all key names as an array
s = first key name as a string [perhaps]
f = first key name as a string [perhaps]
c = count as a number [perhaps]
- Or potentially something like this, which returns an array of all matches by default.
Code: Select all
Object.GetKeys(Needle, Options, FirstKey, LastKey)
ObjGetKeys(Object, Needle, Options, FirstKey, LastKey)
;for comparison:
FoundPos := InStr(Haystack, Needle [, CaseSensitive = false, StartingPos = 1, Occurrence = 1])
FoundPos := RegExMatch(Haystack, NeedleRegEx [, OutputVar, StartingPosition = 1])
FoundPos := StrMatch(Haystack, Needle, Options) ;proposed
FoundPos := StrContains(Haystack, Needle, Options) ;proposed (equivalent to 'contains')
FoundPos := StrExact(Haystack, Needle, Options) ;proposed (equivalent to 'in')
- Other option possibilities (for an options parameter):
- case sensitive/case insensitive
- value is string/numeric
- should it recurse into all subkeys [probably best to omit this]
- handle contains/exact match/starts/ends/RegEx
- apply the search to key names instead of values
- return values instead of key names
- return matches as a string (specify separator)
- Other option possibilities (two parameters):
- first/last key to check
IN GENERAL
- The fact that HasKey exists might lead one to consider HasVal, but HasVal is too limited by itself, and immediately makes you think of GetKeys (Find/List/Match) (GetKeysThatHaveVal), so we immediately face questions reminiscent of InStr / 'StrMatch' / RegExMatch.
- I'm not normally too concerned that a function has all of the key features, because normally I can create a custom function. But with retrieving values from an object, custom functions could potentially be very slow/CPU intensive compared to built-in functions.
- Search functions have to be ambitious. Generally speaking they have to be super-functions, with a large number of options cf. the Sort command. They are usually the functions that pose the most questions when deliberating on the syntax. You have to try and get all the ideas for them out in the open as soon as possible, otherwise you create the wrong function, and you're stuck with it. And you get endless (sensible) wish list requests for additions.
- Here's a checklist of features such functions should probably have, that are relevant also when finding keys:
Improve InStr or IfInString - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 79#p188879
- General points relating to functions that return arrays:
- There is a need to consider consistent syntax/recurring option parameters for functions that return arrays e.g. ControlGetList 'ControlGetItems', RegExMatch, WinGetList, WinGetControls, WinGetControlsHwnd. E.g. StrContains/StrExact/StrMatch functions that I have outlined in the link above.
- Should such functions have first result/delimited list strings as options. Delimited list strings as direct output might sound quirky, however, (a) StrJoin may(?) be slow for thousands of items versus the function appending as it goes, (b) searching an outputted string can be faster than searching an outputted array's values, (c) users commonly want to put a list of results on the clipboard.
- Before you use StrJoin/StrJoinReverse on an array, you need to identify a delimiter character. I had been wanting a function, 'StrUnused', to identify n characters not in any of an object's values (or not in any of the strings passed to it), to be used as delimiters/special delimiters for advanced parsing/temporary characters. A built-in StrUnused ('unused characters') function, that can handle arrays, could potentially be much faster/less CPU intensive than a custom StrUnused function. [EDIT:] Similarly I might want a way to check if a certain character, e.g. CR/LF, appears in any values. A special 'HasVal' search.
- [EDIT:] I've created a prototype ObjGetVals function here:
object get keys/values prototype function - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=43253