你可以迭代存储为scripting.dictionary对象中的项的数组的值吗?

时间:2016-01-16 21:39:42

标签: arrays dictionary vbscript

我想从这个给定的代码中获取返回的值。

  Wscript.Stdout.WriteLine "What is your favorite word? "
        wordReply = wscript.stdin.readline   
  wscript.stdout.writeline "Enter a number: "
        num1 = cint(wscript.stdin.readline)
  wscript.stdout.writeline "Enter a number: "
        num2 = cint(wscript.stdin.readline)
  wscript.stdout.writeline "Enter a number: "
        num3 = cint(wscript.stdin.readline)
  wscript.stdout.writeline "Enter a number: "
        num4 = cint(wscript.stdin.readline)
  wscript.stdout.writeline "Enter a number: "
            num5 = cint(wscript.stdin.readline)
    newWord = wordReply
 set wordReply = new CreateObject("Scripting.Dictionary")

    wordReply.Add newWord, Array(num1, num2, num3, num4, num5)

我想使用Wscript.Stdout.WriteLine返回或显示提示的值,这些值现在位于wordReply的newWord项下键入的数组中。这可以成功完成吗?我已经看过很多关于如何遍历脚本字典对象但从不通过脚本对象值内部的数组的示例。

1 个答案:

答案 0 :(得分:2)

如果字典d包含一个键(“aWord”)下的数组(1,2,3),那么d("aWord")会为您提供副本该数组,可以视为VBScript中的每个数组。证据:

>> Set d = CreateObject("Scripting.Dictionary")
>> d("aWord") = Array(1, 2, 3)
>> WScript.Stdout.WriteLine Join(d("aWord"), "-")
>>
1-2-3
>>

如果你坚持迭代:

>> For i = 0 To UBound(d("aWord"))
>>     WScript.Echo d("aWord")(i)
>> Next
>>
1
2
3