VBScript列表

时间:2012-11-27 13:48:07

标签: vbscript

我试图在VBscript中创建一个简单的列表,但我找不到类似的东西。

基本上,我正在使用Active目录,我需要让用户所属的所有组成为域中所有用户的成员。现在,每个用户可能是不同数量的组的成员,因此我计划使用字典,其中密钥是用户的SAMID,值是他/她所属的所有组的列表。

我可以使用静态数组执行此操作,但是我必须为数组声明一个随机的大尺寸,这是不好的。我最喜欢做的是有一个类似python的列表,我可以简单地做一些像myList.Add这样的事情,不必担心调整大小。

我尝试使用System.Collection.ArrayList,但运行时遇到错误:

PS C:\tmp> cscript.exe .\foo.vbs
Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.

C:\tmp\foo.vbs(1, 1) (null): 0x80131700

我该如何做到这一点?

3 个答案:

答案 0 :(得分:50)

摆脱字典并释放ArrayList的力量。

Option Explicit

dim list
Set list = CreateObject("System.Collections.ArrayList")
list.Add "Banana"
list.Add "Apple"
list.Add "Pear"

list.Sort
list.Reverse

wscript.echo list.Count                 ' --> 3
wscript.echo list.Item(0)               ' --> Pear
wscript.echo list.IndexOf("Apple", 0)   ' --> 2
wscript.echo join(list.ToArray(), ", ") ' --> Pear, Banana, Apple

编辑: 我看到你已经尝试过ArrayList,但是出现了错误。看来你的dotnet框架安装不正确(System.Collections.ArrayList是其中的一部分)。 Microsoft有一篇关于如何解决该问题的文章:http://answers.microsoft.com/en-us/windows/forum/windows_7-performance/error-code-0x80131700/3add8d80-00e0-4355-a994-8630d01c18f5

答案 1 :(得分:9)

Set dic = CreateObject("Scripting.Dictionary")

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

答案 2 :(得分:6)

这也是一个替代方案......我之前创建的实际List类。您可以自由使用/修改它以满足您的需求。我构建的原始类实际上依赖于另一个名为 ArrayIterator 的自定义类。

以下是如何使用它......

Set myList = New List
myList.Add("a")
myList.Add("b")
myList.Add("c")

' Iterate through the List using ArrayIterator. You can of course use other methods...
Set myListItr = myList.GetIterator
While myListItr.HasNext
  MsgBox myListItr.GetNext
Wend

' Iterate through the List by getting the underlying Array.
Dim element
For Each element In myList.GetArray
  MsgBox element
Next

列表类的源代码:

Class List
  Private mArray

  Private Sub Class_Initialize()
    mArray = Empty
  End Sub

  ' Appends the specified element to the end of this list.
  Public Sub Add(element)
    If IsEmpty(mArray) Then
      ReDim mArray(0)
      mArray(0) = element
    Else
      If mArray(UBound(mArray)) <> Empty Then
        ReDim Preserve mArray(UBound(mArray)+1)        
      End If
      mArray(UBound(mArray)) = element
    End If
  End Sub

  '  Removes the element at the specified position in this list.
  Public Sub Remove(index)
    ReDim newArray(0)
    For Each atom In mArray
      If atom <> mArray(index) Then
        If newArray(UBound(newArray)) <> Empty Then
          ReDim Preserve newArray(UBound(newArray)+1)
        End If
        newArray(UBound(newArray)) = atom
      End If
    Next
    mArray = newArray
  End Sub

  ' Returns the number of elements in this list.
  Public Function Size
    Size = UBound(mArray)+1
  End Function

  ' Returns the element at the specified position in this list.
  Public Function GetItem(index)
    GetItem = mArray(index)
  End Function

  ' Removes all of the elements from this list.
  Public Sub Clear
    mArray = Empty
  End Sub

  ' Returns true if this list contains elements.
  Public Function HasElements
    HasElements = Not IsEmpty(mArray)
  End Function

  Public Function GetIterator
    Set iterator = New ArrayIterator
    iterator.SetArray = mArray
    GetIterator = iterator
  End Function

  Public Function GetArray
    GetArray = mArray
  End Function

End Class

ArrayIterator 类的源代码:

Class ArrayIterator
  Private mArray
  Private mCursor  

  Private Sub Class_Initialize()
    mCursor = 0
  End Sub

  Public Property Let SetArray(array)
    mArray = array    
  End Property

  Public Function HasNext
    HasNext = (mCursor < UBound(mArray)+1)
  End Function

  Public Function GetNext
    GetNext = mArray(mCursor)
    mCursor = mCursor + 1
  End Function
End Class