使用脚本字典创建复杂结构 - VBScript
我试图理解一些代码,以便我可以对其进行修改。我对 vbscript 还很陌生,所以非常感谢您的帮助。
我正在查看一个 Dictionary 对象,它的行为就像一个具有由键控制的不同行的表。我不确定发生了什么。命令如 list.add
list.count+1
、 ListElement
现在有点超出我的范围了。该代码能够通过简单的语句输出 1 次使用的数据,
Sub DumpList ( list )
' This function dumps all the people in the office and their information
' in the application window. The list is usually sorted by Name.
Dim e, le
For Each e In list ' output listelements...
Set le = list(e)
LogOutput 0, "line: " & (le.num, 3) & " : " & le.Name _
& " " & le.PHNumber1 & " " & le.PHNumber2 _
& " " & le.Email1 & " " & le.Email2 _
& " " & le.DeskNo.1 & " " & le.OFficeCode _
& " " & le.wirecolour & " " & le.wirecross
Next
End Sub
我不确定它如何对我进行任何更改。
再次感谢
I am trying to understand some code so that i can make modifications to it. I am fairly new at vbscript so any help is appreciated .
I am looking at a Dictionary object that looks like it is behaving like a table with different rows controlled by a key. I am not sure what is happening. commands likelist.add
list.count+1
, ListElement
are a little over my league right now. The code is able to output data for 1 use with simple statement
Sub DumpList ( list )
' This function dumps all the people in the office and their information
' in the application window. The list is usually sorted by Name.
Dim e, le
For Each e In list ' output listelements...
Set le = list(e)
LogOutput 0, "line: " & (le.num, 3) & " : " & le.Name _
& " " & le.PHNumber1 & " " & le.PHNumber2 _
& " " & le.Email1 & " " & le.Email2 _
& " " & le.DeskNo.1 & " " & le.OFficeCode _
& " " & le.wirecolour & " " & le.wirecross
Next
End Sub
I am not sure how it is working for me to make any changes to it.
Thanks again
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
任务:
维护人员集合。
第一次尝试解决方案:
使用带有数字索引=键的字典,基于 dic.Count
正如 sanwar 指出的,字典存储键值对。放置(关于)
字典中的 people,我们需要一个 person 类,从中我们可以创建 person
用于保存多个信息元素的对象/实例。
Person 类的最小/POC 代码:
[类 cPerson 定义/蓝图人员,每个人都有一个 id、一个姓名、一个出生日期和
一份薪水。您可以通过调用 init 函数并传递合适的值来创建人员
姓名、文件和工资成员; id 将自动增强(通过使用全局
计数器而不是在功能更强大的 OO 语言中尽可能使用适当的类级别数据)。]
和一个演示脚本来证明我们可以创建和显示人员:
输出:
现在让我们将它们放入带有数字键的字典中(VBScript 特殊
功能,其他语言有仅限字符串键的字典)基于
.计算财产。空字典的 .Count 属性为 0,通过添加
将第一个元素(包含我们需要的所有信息的 person 对象)添加到字典中
它的 .Count 增加到 1(准备下一次添加)。你很容易就能看到
.Add .Count + 1 是浪费时间/精力:
输出
显示了为什么具有基于 .Count 的数字索引的字典不是解决方案
任务:维护人员集合。
The task:
Maintain a collection of persons.
First attempt at a solution:
Use a dictionary with numerical indices=keys, based on dic.Count
As sanwar pointed out, a dictionary stores key-value pairs. To put (info about)
persons in a dictionary, we need a person class from which we can create person
objects/instances to hold the multiple information elements.
Minimal/POC code for a Person class:
[The class cPerson defines/blue prints persons, each having an id, a name, a dob, and
a salary. You create persons by calling the init function passing suitable values
for name, doc, and wage members; the id will augmented automagically (by using a global
counter instead of proper class level data as possible in more capable OO languages).]
and a demo script to prove we can create and display persons:
output:
Now lets put them in a dictionary with numerical keys (a VBScript special
feature, other languages have string-key-only dictionaries) based on the
.Count property. The .Count property of an empty dictionary is 0, by adding
the first element (a person object holding all info we need) to the dictionary
its .Count increments to 1 (ready for the next addition). You easily can see
that .Add .Count + 1 is a waste of time/effort:
The output
shows why dictionaries with numerical indices based on .Count are no solution
for the task: Maintain a collection of persons.
在本例中,变量
list
包含一个Scripting.Dictionary
。当您在
Scripting.Dictionary
上使用 VBScriptFor Each
构造时,会返回字典中的每个键。因此,在这种情况下,For Each
将在每次迭代时循环将每个键放入变量e
中。Line:-
现在使用此循环的键来查找与该键关联的字典中的值。在这种情况下,该值是一个具有属性的对象。
因此,字典可用于使用其键快速直接地查找“表”中的“行”。此外,正如您发布的代码所示,您可以枚举每个键并查找每个值以“扫描”整个“表”。
In this case the variable
list
holds aScripting.Dictionary
.When you use the VBScript
For Each
construct onScripting.Dictionary
each key in the dictionary is returned. So in this case theFor Each
will loop place each key in the variablee
on each iteration.The Line:-
is now using the key for this loop to lookup the value in the dictionary associated with the key. In this case the value is an object that has properties.
Hence a dictionary can be used to quickly and directly lookup a "row" in a "table" using its key. Also as the code you've posted demonstrates you can enumerate each key and lookup each value to "scan" the whole "table".