在 For...Each 循环的第二次迭代中动态获取 VBScript(经典 ASP)字典对象值时出错

发布于 2024-10-06 09:35:37 字数 2878 浏览 0 评论 0原文

是的,这是一个又长又复杂的标题......抱歉。

我正在使用老式 ASP 中的 VBScript 进行工作。我有一个字典对象,字典中的每个对象都由它的键和作为项目的数组组成。

Dim myDictionary
Set myDictionary = CreateObject("Scripting.Dictionary")

myDictionary.Add "a", Array("a1","a2")
myDictionary.Add "b", Array("b1","b2")
myDictionary.Add "c", Array("c1","c2")

我还将与各种字典条目相对应的字符串列表(并转换为数组)传递到脚本中,以便只有这些条目可以按数组的顺序显示在页面上。

Dim myText
myText = "a, b, c"

Dim myArray
myArray = Split(myText,",")

现在,我想迭代该数组,并显示 myDictionary 中每个相应 Key 的内容。

For Each thing in myArray
    Response.Write myDictionary.Item(thing)(0) & "&nbsp;" & myDictionary.Item(thing)(1) & "<br />" & vbcrlf
Next

它在第一次迭代中完美运行,并正确打印到页面。但在第二次迭代时,我收到错误。这是页面上的完整输出:

a1a2

Microsoft VBScript 运行时错误

“800a000d”类型不匹配:“Item(...)”

/Alpine/en_us/testCase.asp,第 28 行

有人知道为什么这不起作用吗?当然,这里显示的代码只是一个测试用例,但我在我的应用程序中遇到了完全相同的问题。

这是完整的代码,因此您可以将其剪切粘贴到您的测试环境中,如果它可以帮助您帮助我解决这个问题:

<%@LANGUAGE="VBSCRIPT"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Iterating through Dictionary objects - Test Case</title>
</head>

<body>

<%


Dim myDictionary
Set myDictionary = CreateObject("Scripting.Dictionary")

myDictionary.Add "a", Array("a1","a2")
myDictionary.Add "b", Array("b1","b2")
myDictionary.Add "c", Array("c1","c2")

Dim myText
myText = "a, b, c"

Dim myArray
myArray = Split(myText,",")

For Each thing in myArray
    Response.Write myDictionary.Item(thing)(0) & "&nbsp;" & myDictionary.Item(thing)(1) & "<br />" & vbcrlf
Next

%>


</body>
</html>

关于此问题的其他一些有趣的位...

当我对所有三个字典进行硬编码时迭代中的条目,它工作正常:

For Each thing in myArray
    Response.Write myDictionary.Item("a")(0) & "&nbsp;" & myDictionary.Item("a")(1) & "<br />" & vbcrlf
    Response.Write myDictionary.Item("b")(0) & "&nbsp;" & myDictionary.Item("b")(1) & "<br />" & vbcrlf
    Response.Write myDictionary.Item("c")(0) & "&nbsp;" & myDictionary.Item("c")(1) & "<br />" & vbcrlf
Next

产生这个:

a1 a2
b1 b2
c1 c2
a1 a2
b1 b2
c1 c2
a1 a2
b1 b2
c1 c2

并验证 For-Each 循环中的“thing”变量是否有效:

For Each thing in myArray
    Response.Write thing
Next

产生以下内容:

abc

我很困惑...

谢谢大家!我非常感谢您提供的任何帮助。 :-)

干杯,
莱兰多

Yeah, its a long and convoluted title... sorry.

I am working in VBScript in good ol' fashioned ASP. I've got a dictionary object, and each object in the dictionary consists of it's Key, and an Array as the Item.

Dim myDictionary
Set myDictionary = CreateObject("Scripting.Dictionary")

myDictionary.Add "a", Array("a1","a2")
myDictionary.Add "b", Array("b1","b2")
myDictionary.Add "c", Array("c1","c2")

I am also passing into the script a list of strings (and converting to an array), that correspond with various dictionary entries, so that only those entries may be displayed on the page, and in the order of the array.

Dim myText
myText = "a, b, c"

Dim myArray
myArray = Split(myText,",")

Now, I want to iterate through the array, and show the contents of each corresponding Key in myDictionary.

For Each thing in myArray
    Response.Write myDictionary.Item(thing)(0) & " " & myDictionary.Item(thing)(1) & "<br />" & vbcrlf
Next

It works perfectly in the first Iteration, and properly prints to the page. But on the 2nd iteration, I get an error. Here's the full output on the page:

a1 a2

Microsoft VBScript runtime error

'800a000d' Type mismatch: 'Item(...)'

/Alpine/en_us/testCase.asp, line 28

Anyone know why this doesn't work? Naturally, the code shown here is just a test-case, but I am having the exact same problem in my application.

Here is the complete code, so you can just cut-n-paste it into your test environment, if it helps you help me figure this one out:

<%@LANGUAGE="VBSCRIPT"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Iterating through Dictionary objects - Test Case</title>
</head>

<body>

<%


Dim myDictionary
Set myDictionary = CreateObject("Scripting.Dictionary")

myDictionary.Add "a", Array("a1","a2")
myDictionary.Add "b", Array("b1","b2")
myDictionary.Add "c", Array("c1","c2")

Dim myText
myText = "a, b, c"

Dim myArray
myArray = Split(myText,",")

For Each thing in myArray
    Response.Write myDictionary.Item(thing)(0) & " " & myDictionary.Item(thing)(1) & "<br />" & vbcrlf
Next

%>


</body>
</html>

Some other interesting bits about this problem...

When I hard-code all three dictionary entries within the iterations, it works fine:

For Each thing in myArray
    Response.Write myDictionary.Item("a")(0) & " " & myDictionary.Item("a")(1) & "<br />" & vbcrlf
    Response.Write myDictionary.Item("b")(0) & " " & myDictionary.Item("b")(1) & "<br />" & vbcrlf
    Response.Write myDictionary.Item("c")(0) & " " & myDictionary.Item("c")(1) & "<br />" & vbcrlf
Next

Produces this:

a1 a2
b1 b2
c1 c2
a1 a2
b1 b2
c1 c2
a1 a2
b1 b2
c1 c2

And to verify that the 'thing' variable in the For-Each loop works:

For Each thing in myArray
    Response.Write thing
Next

Produces this:

a b c

I'm confused...

Thanks everyone! I really appreciate any help you can provide. :-)

Cheers,
Lelando

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

下壹個目標 2024-10-13 09:35:37

这是因为 myText 中的逗号后面有空格。 Split 函数创建一个包含值“a”、“b”、“c”的数组。您的词典中不存在后两个值。

将行替换

myText = "a, b, c"

myText = "a,b,c"

或将标记分隔符更改为 ", " (注意空格)。

It's because you have spaces after your commas in myText. The Split function creates an array with the values "a", " b", " c". The second two values don't exist in your Dictionary.

Replace the line

myText = "a, b, c"

With

myText = "a,b,c"

Or change your token delimiter with ", " (note the space).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文