C++: AHK source code: append number to array

Talk about things C/C++, some related to AutoHotkey
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

C++: AHK source code: append number to array

Post by jeeswg » 17 May 2019, 11:17

In the AHK v1 source code, this function would return an empty array:

Code: Select all

BIF_DECL(BIF_ReturnEmptyArray)
{
	Object *output_array = Object::Create();
	aResultToken.symbol = SYM_OBJECT;
	aResultToken.object = output_array;
}
What is the best way to append an Integer/Float to that array?

Relevant functions: StrSplit (AHK v1/v2), WinGetList (AHK v2).

Potentially relevant code: Object::Append, which is inside script_object.cpp (AHK v1/v2).

The idea is to create arrays/objects containing integers/floats/strings.

One use would be for a Range function.
traditional for loop: for i = a to b (step c) possibilities - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=43022&p=238737#p238737

Thanks.
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: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: C++: AHK source code: append number to array

Post by nnnik » 20 May 2019, 07:02

A range function should probably create a custom object rather than a built in object with all keys and values inside. You would need insane amounts of Ram for larger loops.
And the lookup cost of a single key value pair is probably greater than calculating the value on the spot.

Im not sure about the code you shared since object creation usually starts with the new operator in most languages.
Recommends AHK Studio

User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: C++: AHK source code: append number to array

Post by jeeswg » 21 May 2019, 17:43

- Fair points re. the Range object, I've updated the post to provide an enumerator object version.
- That said, I tested my original function, and it worked great at 1 million keys, and reasonably well up to 10 million keys. I'd be unlikely to use it for over a thousand keys.

- I don't mind what the C++ code what would like for a built-in Range function. However, the specifics of the start/end/step parameters are subtle, and what I wanted to pin down in the post.

- After writing this post, I saw essentially the same idea here:
Adding help function: TokensToScriptObject and two related macros. by HelgeffegleH · Pull Request #110 · Lexikos/AutoHotkey_L · GitHub
https://github.com/Lexikos/AutoHotkey_L/pull/110
- Both you and Helgef have suggested that being able to easily add keys to an AHK basic object, within the C++ code, could be useful, and I agree.
- I wondered if you had any use cases in mind.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Post Reply

Return to “C/C++”