2012-11-27 3 views
11

VBscript에서 간단한 목록을 만들려고하지만 비슷한 것을 찾을 수 없습니다.VBScript의 목록

기본적으로 저는 Active Directory에서 작업 중이며 도메인 내의 모든 사용자에 대해 사용자가 속한 모든 그룹을 가져와야합니다. 이제는 모든 사용자가 다른 그룹 수의 구성원이 될 수 있으므로 사전에 키를 SAMID로 사용하고 가치는 사용자가 속한 모든 그룹의 목록으로 사용하려고합니다. .

정적 배열을 사용하여이 작업을 수행 할 수 있지만 그다지 좋지 않은 배열의 임의 크기가 크다는 것을 선언해야합니다. 내가 이상적으로하고 싶은 것은 python과 같은 목록을 가지고 있습니다. myList와 같은 일을 간단하게 할 수 있습니다. 추가하고 크기 조정에 대해 걱정할 필요가 없습니다.

나는 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 

가 어떻게 이러한 목표를 달성 할 수 있습니까?

답변

9
Set dic = CreateObject("Scripting.Dictionary") 

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

오른쪽 , 기본적으로 목록을 사전으로 사용하십시오 ... 흠, 가장 깨끗한 것은 아니지만 그렇게 할 것입니다. 건배 – NullPointer

40

사전을 없애고 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

+0

고마워, 나의 첫 번째 본능은 ArrayList를 사용하는 것이었지만 지금은 Dictionary를 사용하여 문제를 해결했다. MS 링크를 보자. 건배 : – NullPointer

+0

'.Item (0)'으로 목록 항목에 접근하려고하는데'Missing Default Property'라는 오류가 발생합니다. arraylist에 .Count를 사용하여 적어도 2 개의 객체가 있음을 확인했습니다. 그게 중요한 것이라면 그것들은 또한 객체이고 원시 타입이 아닙니다. – crush

+0

기본 속성이 아닌 개체 나 기본 속성을 반환하지 않는 개체를 'echo'할 수 없습니다. 어떤 종류의 객체인지 알면'Name'과 같은 적절한 속성을 표시하거나'ToString()'메소드가있는 경우 정보를 얻을 수 있습니다. 또는'wscript.echo typename (list.Item (i))'을 사용하여 이름을 얻을 수 있습니다. – AutomatedChaos

4

다음은 대안입니다 ... 내가 전에 만든 실제 List 클래스. 필요에 따라 자유롭게 사용하거나 수정할 수 있습니다. 내가 만든 원래 클래스는 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 

소스 코드 클래스 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 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 
+0

감사 알렉스, 항목을 추가 할 때마다 동적으로 크기가 조정되는 배열처럼 보이는데, 목록에 대한 좋은 해결책이 될 것입니다. – NullPointer

+1

Remove 함수의 동작이 비정형입니다. 매개 변수로 제공된 색인을 검색합니다. 그런 다음 해당 위치에서 발견 된 값의 모든 항목을 제거합니다. 나는. 이 데이터 (a, b, c, d, e, B, f)를 가정합니다. 제거 (1)은 (a, c, d, e, f)를 반환합니다. 추가 보너스로 메서드에 오류가 있습니다. Size는 5가 아니라 7을 반환합니다. – mgr326639