首页 > 解决方案 > 如何访问VBscript字典中的数组项

问题描述

我在 UFT (VBScript) 中有一个字典对象,其中包含数组项。我想知道如何遍历该数组项中键引用的每个元素。下面是我的字典中包含数组项的示例

Set NodeValues = CreateObject("Scripting.Dictionary")
NodeValues.Add "Nodename", Array("EMS", "ACM") 
NodeValues.Add "MSW", Array("0x31,0x32,0x33,0x34" , "0x25,0x25,0x12,0x12")
NodeValues.Add "SBL", Array("0x35,0x32,0x30,0x31" , "0x45,0x55,0x22,0x92")
NodeValues.Add "Data", Array(array("0x21,0x21,0x21", "0x11,0x11,0x22") , array("0x45,0x55,0x22,0x92","0x25,0x65,0x25")) 

由于这个字典是动态生成的,我不确定它在数组中有多少元素。现在我需要遍历数组中的所有元素以找到我需要的元素例如。如何遍历节点名(键)数组中的所有元素,直到找到 EMS。我尝试了不同的访问方式,但没有成功。

请帮我解决。

标签: dictionaryvbscriptuft14

解决方案


要检查是否在为 key 存储的数组中找到“EMS” Nodename,您可以检查以下代码:

For Each element In NodeValues.Item("Nodename")
    If StrComp(element, "EMS", 1) = 0 Then
        MsgBox "found"
        Exit For
    End If
Next

获取索引:

arrTemp = NodeValues.Item("Nodename")
blnFound = False
For i = 0 To UBound(arrTemp)
    If StrComp(arrTemp(i), "EMS", 1) = 0 Then
        blnFound = True
        Exit For
    End If
Next
If blnFound Then
    MsgBox "Index: " & i
Else
    MsgBox "Not found"
End If

推荐阅读