Please Help!!! Array.GetCapacity() Returns Strange values when trying to create an array in loop

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Nagendra
Posts: 14
Joined: 25 May 2017, 02:23

Please Help!!! Array.GetCapacity() Returns Strange values when trying to create an array in loop

25 May 2017, 02:53

Hi Everyone,

This is my first post in this forum. Thank you all in advance.

For the last few hours I am trying to understand the issue with my script. I am trying to copy an Array into a new array.
The issue is with the "number of Elements in the second array" . Array.GetCapacity() returns undesired values.
I couldn't understand the bug in my script. or I am seriously missing something.. Please help..

Script as follows:

Code: Select all

FirstArray := ["One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten"]
MsgBox, % FirstArray.GetCapacity() ;[b] Returns 10.. Perfect[/b]
SecondArray := Object()
Loop, 10
	{
		SecondArray[A_index] := FirstArray[A_index]
		;MsgBox, % SecondArray[A_index]	[b];Returns All Elements.. Perfec[/b]t 
	}
MsgBox, % SecondArray.GetCapacity()	;[b]Problem Here.. Returns 16[/b]

 
I tried to loop with different numbers from 1 to 10; the results are always either 4 / 8 /16

I appreciate your help.. Thank you
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Please Help!!! Array.GetCapacity() Returns Strange values when trying to create an array in loop

25 May 2017, 03:28

Use SetCapacity on SecondArray first. The capacity of the array doubles each times it auto-expands. It starts at 4 (or 0) then when you try to add element number five, it doubles to 8, and so on...
Alternativetly, you can do this

Code: Select all

SecondArray.Push(FirstArray*)
IMEime
Posts: 750
Joined: 20 Sep 2014, 06:15

Re: Please Help!!! Array.GetCapacity() Returns Strange values when trying to create an array in loop

25 May 2017, 03:48

The Help document, if possible, should be changed.
It makes real confusing, especially for the beginners.

If you want to count the items, you have to use myObject.SetCapacity(0).
On the other hand myObject.GetCapacity(), I do not know what's the real purpose of this method. (I know it returns doubled size at some predefined point(s)).
lexikos
Posts: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: Please Help!!! Array.GetCapacity() Returns Strange values when trying to create an array in loop

25 May 2017, 04:28

Why aren't you using .Length()?

It seems that two of you lack an understanding of the word "capacity".
capacity
kəˈpasɪti/
noun
1.
the maximum amount that something can contain.
There is further (more specific) explanation under SetCapacity.
The maximum number of key-value pairs the object should be able to contain before it must be automatically expanded.
IMEime
Posts: 750
Joined: 20 Sep 2014, 06:15

Re: Please Help!!! Array.GetCapacity() Returns Strange values when trying to create an array in loop

25 May 2017, 04:53

Ok, I'm trying to understand it.

Then, how about put some more brief words. "It is not the count you've expected".

Thanks, always.
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Please Help!!! Array.GetCapacity() Returns Strange values when trying to create an array in loop

25 May 2017, 06:36

It is hard to account for anyones expectations :think:
There is an overlap with this topic. Maybe lexikos has a better answer for that topics question: What is the difference between "" and not_assigned ? (?)

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], joefiesta, mikeyww and 276 guests