Auto-Detect Array Size
#1
Posted 01 September 2007 - 11:01 AM
Dim $array[2] = [0, 0, 0]
I think it would be better if it was possible to just do this.
Dim $array[] = [0, 0, 0]
It would allow one to add items to the array without having to change the size.
#2
Posted 01 September 2007 - 11:04 AM
yes me too! Like in Java.
So long,
Mega
#3
Posted 01 September 2007 - 03:34 PM
You really mean not giving the size as you expect the size be defined by the affectation value ...I always get annoyed when the script crashes because of this.
Dim $array[2] = [0, 0, 0]
I think it would be better if it was possible to just do this.
Dim $array[] = [0, 0, 0]
It would allow one to add items to the array without having to change the size.
I am not sure Jon will love this syntax but if it Java driven ...
#4
Posted 02 September 2007 - 02:18 AM
Say you have this:
Dim $array[2][3][4] = [ _ [ _ [ 1, 2, 3, 4 ], _ [ 5, 6, 7, 8 ], _ [ 9, 10, 11, 12 ] _ ], _ [ _ [ 13, 14, 15, 16 ], _ [ 17, 18, 19, 20 ], _ [ 21, 22, 23, 24 ] _ ] _ ]
Confusing, no? Well maybe not too bad, but only because of the way it's spaced out.
Anyway, if I mixed up a bracket in there, or put too many items in the wrong dimension, I'm going to get an error message telling me so, and I'm going to realize right away what I did wrong, I'll realize I screwed up somewhere. If you have the array auto determine the size it's going to be then it's going to just compensate for mistakes like that and you'll be stuck wondering why your arrays don't have the right stuff in them.
Although as long as we can still manually declare array size that wouldn't bother me at all. Except when I'm trying to help someone debug their code and they have an array like my example here.
#5
Posted 02 September 2007 - 12:15 PM
I can live with current syntax.
#6
Posted 02 September 2007 - 01:32 PM
#7
Posted 04 September 2007 - 12:10 PM
- array size can be pre-checked. For example, C(++) also uses such syntax:
int array[] = { 4, 5, 6 }; char string[] = "blah";
And this is C, where everything is compiled to native machine code. No slowdown.
- I suppose everything in AutoIt is checked at runtime anyways (it's an interpreted thing as you all know).
Therefore I would find this proposed new feature kinda useful. Just a thought
#8
Posted 07 September 2007 - 10:44 PM
That way it's left up to the user how they want their code to work. I'd chalk it up to preference, kind of like using Opt('MustDeclareVars').
#9
Posted 08 September 2007 - 12:29 PM
#10
Posted 08 September 2007 - 01:21 PM
More like Dynamic Arrays. Fixed arrays are not flexible so lack creditability and speed in some usage. Allocating memory for a fixed array maybe fine at first but is ruined once you decide to resize. Dynamic arrays are setup in knowledge that resizing may happen and can lead to better results then a fixed array can under the same conditions. Dynamic arrays are used in JScript, VBScript, C, Java, Lua...Like an Opt('MustSizeArrays')?
#11
Posted 08 September 2007 - 01:30 PM
May I ask how dynamic arrays are implemented in C? (Just curious, nothing more) Maybe you're talking about dynamic strings? In that case be aware that string objects don't use dynamic arrays, they simply reallocate memory...Dynamic arrays are used in JScript, VBScript, C, Java, Lua...
Edited by zatorg, 08 September 2007 - 01:32 PM.
#12
Posted 08 September 2007 - 04:22 PM
Not supported so your question is a good correction to my mistake of adding C to my mentioned list of languages. Like your C example above, C allows you not to specify the amount of values within the array and is allocated during compile time (thus is not dynamic during runtime).May I ask how dynamic arrays are implemented in C? (Just curious, nothing more) Maybe you're talking about dynamic strings? In that case be aware that string objects don't use dynamic arrays, they simply reallocate memory...
Intializing a dynamic array in VBScript for example can simply be done as below.
Dim MyArray()
or
Dim MyArray(1, 2, 3)
without any need to specify size. The Scripting Host will allocate suitable memory storage for the array (without any restrictions imposed by using fixed arrays).
The preference of using Fixed or Dynamic arrays falls down to need and performance. If an array only requires a fixed amount of values then the lower overhead of using a fixed array will boost performance. If resizing is a requirement then dynamic is suitable to use as dynamic sizes the array and preallocates memory (amongst any other optimization) to suit a ReDim of size.
#13
Posted 08 September 2007 - 04:27 PM
#14
Posted 09 September 2007 - 09:47 AM
Shush! If the dev's see someone even hinting at another opt, they're liable to defenestrate you.Like an Opt('MustSizeArrays')?
No, I just meant that it would automatically figure it out based on the way you write the code. If you supply dimensions, it uses, and restricts the array to that info. If not, it auto adjusts. So if you actually write Dim $myArray[3] you'll predefine an array with 3 items, just like we do now, which would be easier on the CPU. But if you leave out the size definitions, then it will automatically create array structure to fit the data you supply.
So in essence, if you predefine your array it will be faster than if you don't. Does that make any sense?
#15
Posted 14 September 2007 - 06:29 PM
Take a look at the malloc, realloc and free functions. Dynamic storage is managed by these functions.May I ask how dynamic arrays are implemented in C? (Just curious, nothing more) Maybe you're talking about dynamic strings? In that case be aware that string objects don't use dynamic arrays, they simply reallocate memory...
#16
Posted 14 September 2007 - 08:36 PM
I am the developer who put the array aggregate initialization into AutoIt. When I was setting this up, I realized I needed to know how big the array was before I started to assign to it. Basically, by the time I was ready to read the array elements, I already had to have the array built, ready to be loaded with the data. So leaving out the dimension values was not going to work.I always get annoyed when the script crashes because of this.
Dim $array[2] = [0, 0, 0]
I think it would be better if it was possible to just do this.
Dim $array[] = [0, 0, 0]
It would allow one to add items to the array without having to change the size.
I did experiment with allowing an empty array dimension, and determining the size of the dimension by the size of the initialization data set, but it more than doubled the time and memory requirements to create and initialize the array.
In C (or other compiled languages), the data is recognized and the array size is determined during compiling, but the data is not actually assigned until run-time. During run-time, the array and its initializer sizes are already known, so you do not lose any time determining it, just allocate the array and assign the data. AutoIt, even when "compiled" into an EXE, is interpreted. This means that the array size would have to be determined each time and then the array could be sized and then the data could be inserted. This just takes way too long and eats gobs of memory, more than twice the array to be assigned. It also needed a lot of code to do, and we were trying to keep the entire AutoIt less than 100 KB at the time. I know it is a little bigger now, but still it is mighty small.
#17
Posted 15 September 2007 - 04:38 AM
@Saunders: How are they supposed to throw me out of the window of a forum?
#18
Posted 15 September 2007 - 06:54 AM
On a completely separate note, when did Nutster come back?
#19
Posted 19 September 2007 - 04:47 PM
Last week. The project that I was working on for the last two years is finally done and I find I have free time again!Hey man they're the developers.. I'm sure they'll find a way.
On a completely separate note, when did Nutster come back?
#20
Posted 19 September 2007 - 05:14 PM
When you ReDim, the new array is created and then all the old data is copied, element by element, to the corresponding elements of the new array. Because of this, you do not want to be ReDim'ming arrays too often. Grow your arrays several elements at a time, to reduce the number of Redim calls you make. This is another part of the array system that I wrote, so I know how this works (if my memory is still operating after all this time).All "dynamic" arrays are just reallocated every time they are resized. It doesn't matter what the source looks like, deep down, it is still a reallocation.
@Saunders: How are they supposed to throw me out of the window of a forum?
Throw you out? Hmm, let's see. Are you on the banned list?
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users




