Dynamic arrays in VBScript with Split (). Is there a better way?

Many scripts that I write at my work depend on the creation of dynamically significant arrays. Arrays in VBScript make this a rather difficult task, since everyone needs Redimarrays every time you want to resize them. To get around this, I started creating comma-separated lines and used Split(...)1D arrays to create them. Although this works fantastic for me, I wondered if VBScript has a more efficient way to handle this. So I ask StackOverflow; there is?

Disclaimer . I fully understand that VBScript is a rather non-standard scripting language, but Python requires additional software, which makes server automation a bit difficult, and PowerShell is not the core component yet. I study them both though!

+3
source share
2 answers

The solution that I usually use is to resize the array every time I add a new element to it. Thus, the final 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)

Another solution suggested by Carlos is to do this with a dictionary, which is probably a cleaner solution:

Set dic = CreateObject("Scripting.Dictionary")

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

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

Thanks Maciej

+5

?

+1

Source: https://habr.com/ru/post/1759006/


All Articles