VBScript中的列表

VBScript中的列表,vbscript,Vbscript,我试图在VBscript中创建一个简单的列表,但找不到类似的内容 基本上,我正在使用Active directory,我需要为域中的所有用户获取用户所属的所有组。现在,每个用户可能都是不同数量组的成员,因此我计划使用字典,其中键是用户的SAMID,值是他/她所属的所有组的列表 我可以用一个静态数组来实现这一点,但是我必须为数组声明一个随机的大大小,这是不好的。理想情况下,我想做的是创建一个类似python的列表,在这里我可以简单地执行myList.Add之类的操作,而不必担心大小问题 我尝试使用

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

基本上,我正在使用Active directory,我需要为域中的所有用户获取用户所属的所有组。现在,每个用户可能都是不同数量组的成员,因此我计划使用字典,其中键是用户的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

我怎样才能做到这一点呢?

摆脱字典,释放ArrayList的威力

Set dic = CreateObject("Scripting.Dictionary")

dic.Add "Item1", ""
dic.Add "Item2", ""
dic.Add "Item3", ""
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 framework的安装似乎不正确(System.Collections.ArrayList就是其中的一部分)。微软有一篇关于如何解决这个问题的文章:

这里还有一个替代方案。。。一个我不久前创建的实际列表类。您可以自由使用/修改它以满足您的需要。我构建的原始类实际上依赖于另一个名为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
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
类列表
私人玛丽酒店
私有子类_Initialize()
玛丽=空的
端接头
'将指定的元素追加到此列表的末尾。
公共子添加(元素)
如果我是空的(玛丽),那么
雷迪姆马瑞酒店(0)
mArray(0)=元素
其他的
如果mArray(UBound(mArray))为空,则
雷迪姆马里雷酒店(乌邦(马里雷)+1)
如果结束
mArray(UBound(mArray))=元素
如果结束
端接头
'删除此列表中指定位置的元素。
公共子目录(索引)
ReDim新阵列(0)
对于mArray中的每个原子
如果原子mArray(索引)那么
如果newArray(UBound(newArray))为空,则
ReDim保留新数组(UBound(新数组)+1)
如果结束
newArray(UBound(newArray))=原子
如果结束
下一个
mArray=newArray
端接头
'返回此列表中的元素数。
公共功能规模
尺寸=UBound(mArray)+1
端函数
'返回此列表中指定位置的元素。
公共函数GetItem(索引)
GetItem=mArray(索引)
端函数
'删除此列表中的所有元素。
公共分区清除
玛丽=空的
端接头
'如果此列表包含元素,则返回true。
公共职能要素
HasElements=不为空(mArray)
端函数
公共函数GetIterator
Set迭代器=新数组迭代器
iterator.SetArray=mArray
GetIterator=迭代器
端函数
公共函数GetArray
GetArray=mArray
端函数
末级
ArrayIterator类的源代码:

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
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
类数组迭代器
私人玛丽酒店
二等兵麦库索
私有子类_Initialize()
mCursor=0
端接头
公共属性Let SetArray(数组)
mArray=数组
端属性
公共职能下一步
HasNext=(mCursor
是的,所以基本上使用字典作为列表…嗯,不是最干净的,但我想它可以。干杯汉克斯,我的第一反应是使用ArrayList,但现在我已经用字典解决了这个问题。我将查看MS链接,cheers:)我正在尝试使用
访问列表项。项(0)
但是我得到一个错误
缺少默认属性
。我已使用
.Count
确认arraylist中至少有2个对象。它们也是对象,而不是本机类型,如果这很重要的话。您不能
echo
没有默认属性或没有返回基元的默认属性的对象。如果您知道它是什么类型的对象,您可以显示它的相应属性,如
Name
,或者如果它有
ToString()
方法,则可以获取信息。或者,您可以使用
wscript.echo typename(list.Item(i))
获取其名称。这确实需要安装.NET,虽然大多数Windows计算机都是这样,但在某些情况下可能没有安装。这使脚本依赖于.NET 3.5。感谢Alex,它看起来像一个数组,每次添加一个项时都会动态调整大小,对于列表来说,这是一个很好的解决方法。Remove函数的行为是非典型的。它搜索作为参数提供的索引。然后,它将删除在该位置找到的值的所有匹配项。即,假设该数据(a、B、c、d、e、B、f)。移除(1)将返回(a、c、d、e、f)。作为额外的奖励,该方法包含一个错误:Size返回7,而不是5。