21

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

どうすればこれを達成できますか?

4

3 に答える 3

65

辞書を取り除き、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

于 2012-11-27T15:52:38.727 に答える
12
Set dic = CreateObject("Scripting.Dictionary")

dic.Add "Item1", ""
dic.Add "Item2", ""
dic.Add "Item3", ""
于 2012-11-27T13:54:11.733 に答える
7

これも代替案です...私が少し前に作成した実際の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

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

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
于 2012-11-28T16:36:12.380 に答える