VBScript 中使用 Split() 的动态数组。有更好的办法吗?

发布于 2024-09-13 22:37:46 字数 338 浏览 3 评论 0原文

我在工作中编写的许多脚本都依赖于动态大小数组的创建。 VBScript 中的数组使这是一项相当艰巨的任务,因为每次想要调整数组大小时都必须对其进行 Redim。为了解决这个问题,我开始制作逗号分隔的字符串,并使用 Split(...) 创建一维数组。虽然这对我来说非常有用,但我想知道 VBScript 是否有更有效的方法来处理这个问题。所以我问 StackOverflow;有吗?

免责声明:我完全知道 VBScript 是一种相当不标准的脚本语言,但 Python 需要额外的软件,这对于服务器自动化来说有点麻烦,而且 PowerShell 还不是核心组件。不过,我正在学习它们!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苏别ゝ 2024-09-20 22:37:46

我通常采用的解决方案是每次向数组添加新项目时调整数组大小。这样,结束数组将永远不会有任何未使用的条目。

ReDim aArray(-1)

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

MsgBox Join(aArray, "," & vbNewLine)

卡洛斯提出的其他解决方案是使用 Dictionary 对象来完成它,这可能是更干净的解决方案:

Set dic = CreateObject("Scripting.Dictionary")

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

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

谢谢,
马切伊

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

骷髅 2024-09-20 22:37:46

字典 对象怎么样?

How about a Dictionary object?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文