Creating a user defined function to search in an Array

Discuss the future of the AutoHotkey language
User avatar
jeeswg
Posts: 6648
Joined: 19 Dec 2016, 01:58
Location: UK

Creating a user defined function to search in an Array

14 Apr 2017, 15:28

[In response to:]
Object.HasValue/Contains/KeyOf/FindValue - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 37&t=30536

I have been working on my own functions for this.

I have been thinking of whether there should be an options parameter:
- case sensitive
- return first match/all matches/position of first match/positions of all matches/number of matches
- needles are: values/names of keys/a delimiter-separated string/values as delimiter-separated strings
- return delimiter-separated string/array of key names
- specify delimiter(s), possibly by Ord number(s)
- (an additional possible parameter:) check whether haystack contains none/all of needles (i.e. this can make the function give up earlier and hence run faster)

I see the overall template as:
does [A] match/contain/start/end

Where A or B can be:
- a variable / a comma-separated list (a string)
- an array's key names
- an array's values (key contents)
- content that is not a string (key contents)

Anyway, I'm thinking that I may take my 4 StrIn/StrContains/StrStarts/StrEnds functions, and boost them with object/array support, either for needles only (in which case separate Array functions), or to support strings *and* arrays/objects.

At the moment I have:

Code: Select all

;Note: It is difficult to choose between default: vDelim="," or vDelim="", I believe that vDelim="" (no delimiters) is a better assumption, because if commas are a delimiter by default this can cause surprises. I might propose JEE_StrContainsC, JEE_StrContainsP functions, where comma/pipe are default delimiters for convenience.

JEE_StrContains(ByRef vText, ByRef vNeedles, vDelim="", vCaseSen=0, ByRef vMatch="")
{
	if (vDelim = "")
		if InStr(vText, vNeedles, vCaseSen)
		{
			if IsByRef(vMatch)
				vMatch := vNeedles
			return 1
		}
		else
			return 0

	Loop, Parse, vNeedles, % vDelim
		if InStr(vText, A_LoopField, vCaseSen)
		{
			if IsByRef(vMatch)
				vMatch := A_LoopField
			return A_Index
		}
	return 0
}

;==================================================

;early days here (I might have done more, but was undecided on how to specify key names v. value contents as the haystack, while keeping the parameters consistent with StrContains)
JEE_ArrayContains(oArray, vNeedle)
{
	for vKey, vValue in oArray
	{
		if InStr(vValue, vNeedle)
			return A_Index
	}
	return 0
}
Btw I see options for string starts/ends as highly useful, something I use far more frequently than other existing AHK commands, and something I felt was needed from very early on when I first started using AutoHotkey.
topic split because it is totally off topic
Last edited by jeeswg on 16 May 2017, 10:12, edited 3 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
just me
Posts: 6464
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Object.HasValue/Contains/KeyOf/FindValue

14 Apr 2017, 15:46

Hi jeeswg,

you already posted this functions. Any news about your 'Gui without Gui-commands' concept?
User avatar
jeeswg
Posts: 6648
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Creating a user defined value to search in an Array

14 Apr 2017, 16:37

JEE_StrContains is updated and JEE_ArrayContains is new (although simple at the moment).

Oh, thanks for showing an interest in 'GUIs via DllCall'. Could be done within the next few weeks. I have about 4 almost-finished projects.

Btw I think the post above was pretty relevant re. the key/value search functions for arrays. The point was to illustrate some of the concerns and approaches. I could have worded it to sound more obviously on-topic, e.g. 'I had similar concerns regarding 'Object.HasValue/Contains/KeyOf/FindValue', when creating my own equivalent ...'.

==================================================

Btw it would be useful if you could help me with these, if you happen to know the answers, although they are problems even if you use the AHK v1 Gui commands (and most likely the AHK v2 Gui commands also).

[key combinations that cause beeps in a GUI, how to properly define hotkeys for a GUI]
[beeps + hotkeys]
send keys to a GUI window/control without causing beeps (+ the standard way to define an AHK GUI hotkey) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=29940

[how AutoHotkey handles multiple GUIs or MsgBoxes/InputBoxes, since multithreading is not possible]
[threads]
An attempt at a script that creates MsgBoxes/InputBoxes for other scripts - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 53#p138253

Cheers.

==================================================

If more people were forthcoming on my questions regarding: 4 types of array, ObjAddRef, asm to mcode, and on shell extensions (which I haven't asked about yet), then I might be further along with things. It's odd when it seems that nobody on the forums knows the answers e.g. re. shell extensions and asm!

[objects - ObjAddRef and ObjRelease]
conversion logic, v1 = -> v1 := -> v2, two-way compatibility - Page 4 - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 36#p138636

[objects - 4 types of array (alphabetical/creation order, case sensitive/insensitive)]
case sensitive/case insensitive arrays (maintain key order, no key autosort) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=29198

[asm files to machine code for use in AHK functions][maybe AHK Unicode needs an internal ANSI version of RegEx]
InBuf function currently 32-bit only (machine code binary buffer searching) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=28393

[shell extensions]
DllCall - Assign icon for “Copy/Cut/Paste/Delete” Windows default context menu items - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=30377
summarize total size property box - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=30483
Shell extension to show cover icons of media files? - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=11533
Creating a DLL Shell Extension with AutoHotkey? - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=11895
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
nnnik
Posts: 4242
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Creating a user defined function to search in an Array

14 Apr 2017, 18:03

Your post is not relevant because the API we are discussing supports:
Searching for objects that are stored within arrays, searching for numbers that are stored within arrays and also searching for strings within arrays.
We are discussing the matter as to how we solve the problem of returning the boolean value if the value is contained in the array at all, how to return the key of such a value depending on whether it is only contained once or multiple times.
Your function covers only bits of that and it is safe to say has nothing to do with the solution to this problem.

The issue with ASM code is that you need 2 Versions for 32 and 64 bit machines and eventually need to write both Versions.
Compared to the effort you need to take to create something like that, the speed gain is only minor and it can be considered niche.
It doesn't work with very complex scripts since they are troublesome to link and to structure. Code reusage is almost 0.

For ObjectAddRef and ObjRelease I can explain.
Objects and Arrays are not like Strings. Unlike strings and numbers they exist as an independent thing from their variables.

Code: Select all

a := b := { abc:1 }
a.abc := 2
Msgbox % b.abc 
However if they really would exist totally independent you would run into several problems for longer running scripts:
Cosider this code:

Code: Select all

Loop
a := []
This creates an infinite amount of arrays. If every array would keep existing even after it isn't needed anymore we would run out of memory.
So the solution is to delete every Array that isn't used anymore. But how do we define that? The answer to that is simple. We count every variable refering to the object and if that count is 0 it isn't needed anymore.
However there are instances which AutoHotkey can't count. To be more specific thse are Object pointers.
You might find yourself wanting to pass an object to a dll function. For that you need a reference dlls can understand. Those references are pointers. However they are a reference towards the object but are not exactly a variable.
So what AutoHotkey does is give you some control over adding and releasing references or increasing and decreasing the count of variables I mentioned before. You know need to control when the reference is freed or add 1 if another reference is added.
Recommends AHK Studio
User avatar
jeeswg
Posts: 6648
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Creating a user defined function to search in an Array

14 Apr 2017, 18:45

Thank you, I want to look into ObjAddRef and ObjRelease, and the 4 types of array more before asking more questions on them. Unless someone wants to some of my ObjAddRef questions at the link, or provide some tips on perfecting the 4 arrays I was working on. However, what could also be useful, is a demonstration script, that does many common things with objects, while adding comments discussing what is happening to the reference counter. Or if there are some especially good links, I could look at them (MSDN ones are unlikely to be good for explaining concepts). Also, if there are any good examples of quite simple classes but with lots of methods and properties, including Enum, that could be quite interesting.

If you want to search for ANSI/UTF-8 strings or even UTF-16 strings that could be at odd or even offsets, in binary data, RegExMatch is not very good for this, especially for needles of few bytes in size. In the last few days I have been thinking that perhaps RegEx in AutoHotkey Unicode should have ANSI support. Since I have the asm file for the 32-bit version of InBuf for binary searching, it may(?) be relatively easy to convert that asm file to a 64-bit version. Some key AutoHotkey custom functions (maybe even built-in ones) use machine code like GDIP_ImageSearch, for doing tasks as rapidly as possible. I also think that machine code/assembler is an important thing to know about, in terms of general computing knowledge, and starting to understand what's really going on within computers. One of those times when I would program without immediate utility in mind.

One thing I should have said, about my StrContains function, is that it provides a template for what a user-friendly ArrayContains function could look like, with the simplicity of 'if var contains' as an inspiration.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “AutoHotkey v2 Development”

Who is online

Users browsing this forum: No registered users and 9 guests