views:

105

answers:

2

Hi, all!

A lot of the scripts I write at my job depend on the creation of dynamically-sizable arrays. Arrays in VBScript make this a pretty arduous task, as one has to Redim arrays every time one wants to resize them. To work around this, I've started making comma-delimited strings and using Split(...) to create 1D arrays out of it. While this works fantastic for me, I've wondered whether VBScript has a more efficient way of handling this. So I ask StackOverflow; are there?

Disclaimer: I'm fully aware that VBScript is a pretty substandard scripting language, but Python requires extra software, which is a bit of a hassle for server automation, and PowerShell isn't a core component yet. I'm learning them both, though!

A: 

How about a Dictionary object?

aphoria
I was going to suggest that. It seems to be the answer to a lot of problems.
Tester101
I use Dictionaries when I want k/v pairs. The problem I was referring to was for cases when I need 1D arrays.
Carlos Nunez
You can certainly use it like a 1D array.
aphoria
A: 

The solution I usually go for is to resize the array each time I add new item to it. In that way the end array will never have any unused entries.

ReDim aArray(-1)

For i = 1 To 10
    ReDim Preserve aArray(UBound(aArray) + 1)
    aArray(UBound(aArray)) = i
Next

MsgBox Join(aArray, "," & vbNewLine)

Other solution proposed by Carlos is to do it using Dictionary object which is probably cleaner solution:

Set dic = CreateObject("Scripting.Dictionary")

dic.Add "Item1", ""
dic.Add "Item2", ""
dic.Add "Item3", ""

msgbox Join(dic.Keys, "," & vbNewLine)

Thanks, Maciej

Maciej Zaleski