VBScript列表

Nul*_*ter 16 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
Run Code Online (Sandbox Code Playgroud)

我怎么能做到这一点?

Aut*_*aos 52

摆脱字典并释放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
Run Code Online (Sandbox Code Playgroud)

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

  • 这确实需要安装 .NET,虽然大多数 Windows 计算机都是这样,但在某些情况下可能不需要安装。只是根据您的用例需要注意一些事情。 (3认同)
  • 这使您的脚本依赖于 .NET 3.5 (3认同)

Chi*_*ung 9

Set dic = CreateObject("Scripting.Dictionary")

dic.Add "Item1", ""
dic.Add "Item2", ""
dic.Add "Item3", ""
Run Code Online (Sandbox Code Playgroud)


Ale*_* Fu 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
Run Code Online (Sandbox Code Playgroud)

List类的源代码:

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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)