"Your organization doesn't allow you to download, print, or sync using this device. To use these actions, use a device that's joined to a domain or is marked compliant. For help, contact your IT department."
' Dictionary object 선언
Dim myDictionary
Set myDictionary = CreateObject("Scripting.Dictionary")
' key-value 등록법
myDictionary.Add "a", Array("1-2", "2-3", "3-4")
myDictionary.Add "b", Array("5-6", "6-7", "7-8")
myDictionary.Add "c", Array("9-10", "10-11", "11-12")
' value호출 방법
msgbox "Value associated with 'a':"
For Each item In myDictionary("a")
msgbox item
Next
msgbox "Value associated with 'b':"
For Each item In myDictionary("b")
msgbox item
Next
msgbox "Value associated with 'c':"
For Each item In myDictionary("c")
msgbox item
Next
그림으로 그려보자면 아래와 같다.
결과 확인
아래와 같은 msgbox가 순서대로 출력된다.
(나머지 생략)
즉 Key값 먼저 출력되고 그 뒤에 value값이 따라서 출력된다.
이를 통해 VBScript에서 어떻게 Dictionary를 쓰고 Key값 호출, Value값 호출을 하는지 보고 수정해서 쓰면된다.
위 예문에서는 For Each문을 통해 한꺼번에 모두다 출력하도록 했지만, 만약 List 내부의 특정 값에 접근하고 싶다면 아래와 같이 접근한다.
' Create a new Dictionary object
Dim myDictionary
Set myDictionary = CreateObject("Scripting.Dictionary")
' Add key-value pairs to the dictionary
myDictionary.Add "a", Array("1-2", "2-3", "3-4")
' Access the first item in the array associated with "a" key
Dim firstItem
firstItem = myDictionary("a")(0)
' Display the first item using MsgBox
MsgBox "First item associated with 'a' key: " & firstItem
(결과)
* 만약에 생성한 "a" Dictionary에 있는 Array에 추가로 값을 더 넣고 싶다면 아래 샘플 코드...
->
이 코드에서는 existingArray 변수에 "a" 키에 연결된 배열을 가져와서, 새로운 값을 추가하려는 newArray와 결합하여 combinedArray에 저장합니다.
그런 다음 "a" 키에 새로운 combinedArray를 할당합니다. 이렇게 하면 오류 없이 값을 추가할 수 있습니다.
' Create a new Dictionary object
Dim myDictionary
Set myDictionary = CreateObject("Scripting.Dictionary")
' Add key-value pairs to the dictionary
myDictionary.Add "a", Array("1-2", "2-3", "3-4")
' Access the first item in the array associated with "a" key
Dim firstItem
firstItem = myDictionary("a")(0)
' Get the existing array associated with "a" key
existingArray = myDictionary("a")
' Create a new array with additional values
Dim newArray
newArray = Array("apple", "banana", "orange")
' Combine the existing and new arrays
ReDim combinedArray(UBound(existingArray) + UBound(newArray) + 1)
Dim i
For i = 0 To UBound(existingArray)
combinedArray(i) = existingArray(i)
Next
For i = UBound(existingArray) + 1 To UBound(existingArray) + UBound(newArray) + 1
combinedArray(i) = newArray(i - UBound(existingArray) - 1)
Next
' Update the "a" key in the dictionary with the combined array
myDictionary("a") = combinedArray
' Output verification
MsgBox "Values associated with 'a':"
For Each item In myDictionary("a")
MsgBox item
Next