Dynamic Arrays

A dynamic array does not have a fixed size. It can be enlarged or shrunk as needed by code during program execution. A dynamic array is declared by using empty parentheses in the Dim statement.

Before using the array, you must set its size using the ReDim statement.

When you use ReDim, any data in the array is normally lost. You can preserve existing array data (with some limitations) by including the Preserve keyword in the ReDim statement.

IDEAScript Language:


I often use the ReDim statement in my code because i am used to it from other languages, funny thing the Idea-Editor sometimes changes my ReDim back to a normal Dim statement.

Brian Element's picture

Hi Florian, that happens if the variable has not been properly Dimed, so if you have the following code:

Sub Main

     Dim variable() as string

     Redim variable1(14)

end sub

If you run the above the Redim will change to a dim as I mistyped the variable name.  For the Redim to work the variable has to be dimensioned first.

I always need to congratulate you for all benefits you give us with your inicitive! The IDEA Help has "IDEAScript does not support Dynamic arrays." researching by ARRAY.  When I research by REDIM, They mentioned that is possible "The ReDim statement is used to size or resize a dynamic array that has already been declared using the DIM statement with empty parentheses". Are they lost their mind? Where can I see about REDIM "PRESERVE" over IDEAScript help or IDEA Help? Thanks at all!

Brian Element's picture

You can use the preserve for single dimension arrays (it won't work for multi-dimensional arrays Frown).  The usual syntax is the following:

Dim sArray() as string

Redim sArray(5)

If you want to add an additional item to the array you could do this.

ReDim Preserve sArray(UBound(sArray) + 1)     

This would add one item to the array.

As far as finding insturctions, I am pretty sure it is part of the IDEAScripting training but I don't think the online documentation mentions it.

Thanks Brian.
Could you type one solution by adding fields, I have a process that work in record by record. Instead of Array solution (that's limited by 1 dimention), when I need multidimension variable? Which Object is related for this task?

Brian Element's picture

Well there are probably a few solutions.

If you want to use a multi-dimension array you can but you have to define it to the maximum number of elements at the very beginning, the preserve function only seems to work for 1 dimension arrays, so if you know that you will have a 2 dimensional array with 500 items you would define it at the start of the program:

Dim aMultiDim(500, 500) as string

If you need something larger you could have several single dimensional arrays to hold the variables, in that case you could use the redim and preserve functions.

So if your file has 3 fields you would set-up three arrays, one for each field:

Dim aField1() as string

Dim aField2() as string

Dim aField3() as string

Then you would then just redim them when you want to add some variables to them.

Hope this helps.