0

I can't create an undefined array in VBS.

It should be as long as needed. I don't want to declare some limit like 10 or so, with Dim arrnode(10). I need an unlimited one.

I tried:

Dim arrnode    
arrnode(1) = "a"     
arrnode(2) = "b"     
arrnode(3) = "c"     
arrnode(4) = "d" 

And lots of variations like:

Dim arrnode()   
Dim arrnode() As Variant
Dim arrnode() As Variant()

Etc.etc. None of this works. Errors only.

Please give an example that works. I'm not looking for an explanation as I can't understand the "variant" explanation I found over and over on the web. I'm just desperately in need of a very simple array that works.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Geoff Vane
  • 33
  • 1
  • 8

2 Answers2

1

To create a static (non-resizable) array with 10 slots use

Dim arr(9)

To create a dynamic (resizable) array with 10 slots use

ReDim arr(9)

In each case the argument is the upper boundary of the array. Since VBScript arrays are zero-based (indexes start with 0) that is the number of elements minus one.

If you want to append to an array without knowing the required size beforehand create it as an empty dynamic array:

ReDim arr(-1)

For appending to that array you need to increase its size first:

ReDim Preserve arr(UBound(arr)+1)
arr(UBound(arr)) = "a"

ReDim Preserve arr(UBound(arr)+1)
arr(UBound(arr)) = "b"

...

You could wrap this in a procedure to simplify the handling a little:

Sub AppendToArray(ByRef a, v)
    ReDim Preserve a(UBound(a)+1)
    a(UBound(a)) = v
End Sub

AppendToArray arr, "a"
AppendToArray arr, "b"
...

Beware, though, that resizing an array while preserving its content is a slow operation, because VBScript creates a new array with increased size, moves the content of the existing array, and then replaces the existing array with the new one.

VBScript does not support dynamically appending to an array otherwise. As an alternative you could use the System.Collections.ArrayList class, which does support dynamic resizing:

Set arr = CreateObject("System.Collections.ArrayList")

arr.Add "a"
arr.Add "b"
...
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • Thanks for the swift reply. I shall have to work with these tools. It would be nice to place a constant in the beginning of the script that declares the array size. So far dim(c) is not working. Maybe the ReDim eats a constant. – Geoff Vane Aug 10 '18 at 12:14
  • `Dim` only works with literals. But as I wrote, the resulting array would not be resizable anyway, so that's not what you want. `ReDim` does work with variables and constants as well. – Ansgar Wiechers Aug 10 '18 at 12:17
  • I was in the middle of giving an example of using a Dictionary as an array-like object, then I saw your post. ArrayList would really be the better way to go. +1 – Tim Aug 10 '18 at 14:41
  • @Tim no need when you could just look at one of the [existing ones](https://stackoverflow.com/a/17664578/692942) *(btw Ansgar, that links one of yours)*. – user692942 Aug 12 '18 at 12:02
0

There are different ways to create arrays in VBScript

Arrays with defined size

Dim arrTest1(3)
arrTest1(0) = "1"
arrTest1(1) = "2"
arrTest1(2) = "3"
arrTest1(3) = "4"

Arrays without defined size

Dim arrTest2()
ReDim arrTest2(3)
arrTest2(0) = "1"
arrTest2(1) = "2"
arrTest2(2) = "3"
arrTest2(3) = "4"

Using Array function to create an array

Dim arrTest3
arrTest3 = Array ("1", "2", "3", "4")

In VBScript, arrays starts from zero and you can't define the variable type (e.g. as Variant) while declaring arrays

You can use the second option if you are not sure about the size of array. You can always ReDim it later on to change its size. Don't forget to use Preserve keyword if you want to protect the array data if you are ReDiming it

Pankaj Jaju
  • 5,371
  • 2
  • 25
  • 41
  • Thanks for the swift reply. It seems one way or another I'll have to define a size. Strange thing is, I once did a string split in 2 parts, into variable x with the Split command and I automatically got an array with x(0) and x(1). I never did a dim X() or something like that. So that went far more easy than expected while creating an array myself, seems quite difficult. I'm just gonna have to create an Const at the beginning of the script to define the array size. I give up! – Geoff Vane Aug 10 '18 at 12:00
  • 1
    `Split` will return an array. You can create a script where you can ask the users how many values they want to enter and create an array based on that. Or use `Dictionary` objects which are more flexible. – Pankaj Jaju Aug 10 '18 at 12:02
  • Most people use dictionaries instead of arrays. – CatCat Aug 11 '18 at 19:52
  • You’re just regurgitating old answers, instead flag as a duplicate and move on. – user692942 Aug 12 '18 at 12:00