使用脚本字典创建复杂结构 - VBScript

发布于 2024-11-28 09:04:28 字数 921 浏览 1 评论 0原文

我试图理解一些代码,以便我可以对其进行修改。我对 vbscript 还很陌生,所以非常感谢您的帮助。

我正在查看一个 Dictionary 对象,它的行为就像一个具有由键控制的不同行的表。我不确定发生了什么。命令如 list.add list.count+1ListElement 现在有点超出我的范围了。该代码能够通过简单的语句输出 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 like
list.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 技术交流群。

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

发布评论

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

评论(2

温柔戏命师 2024-12-05 09:04:28

任务:
维护人员集合。

第一次尝试解决方案:
使用带有数字索引=键的字典,基于 dic.Count

正如 sanwar 指出的,字典存储键值对。放置(关于)
字典中的 people,我们需要一个 person 类,从中我们可以创建 person
用于保存多个信息元素的对象/实例。

Person 类的最小/POC 代码:

Dim g_nPersonId : g_nPersonId = -1
Class cPerson
  Private m_nId
  Private m_sName
  Private m_dtBirth
  Private m_dWage
  Public Function init( sName, dtBirth, dWage )
    Set init    = Me
    g_nPersonId = g_nPersonId + 1
    m_nId       = g_nPersonId
    Name        = sName
    Birth       = dtBirth
    Wage        = dWage
  End Function  
  Public Property Let Name(  sName   ) : m_sName   = sName     : End Property
  Public Property Let Birth( dtBirth ) : m_dtBirth = dtBirth   : End Property
  Public Property Let Wage(  dWage   ) : m_dWage   = dWage     : End Property
  Public Property Get Id()             : Id        = m_nId     : End Property
  Public Property Get Name()           : Name      = m_sName   : End Property
  Public Property Get Birth()          : m_dtBirth = m_dtBirth : End Property
  Public Property Get Wage()           : m_dWage   = m_dWage   : End Property
  Public Function Data()
    Data = Array( m_nId, m_sName, m_dtBirth, m_dWage )
  End Function  
End Class ' cPerson

[类 cPerson 定义/蓝图人员,每个人都有一个 id、一个姓名、一个出生日期和
一份薪水。您可以通过调用 init 函数并传递合适的值来创建人员
姓名、文件和工资成员; id 将自动增强(通过使用全局
计数器而不是在功能更强大的 OO 语言中尽可能使用适当的类级别数据)。]

和一个演示脚本来证明我们可以创建和显示人员:

  Dim oAdam : Set oAdam = New cPerson.init( "Adam", #1/5/2001#, 1234.56 )
  Dim oEve  : Set oEve  = New cPerson.init( "Eve" , #1/6/2001#, 6543.21 )
  Dim oPerson
  For Each oPerson In Array( oAdam, oEve )
      WScript.Echo Join( oPerson.Data(), " - " )
  Next    

输出:

0 - Adam - 1/5/2001 - 1234.56
1 - Eve - 1/6/2001 - 6543.21

现在让我们将它们放入带有数字键的字典中(VBScript 特殊
功能,其他语言有仅限字符串键的字典)基于
.计算财产。空字典的 .Count 属性为 0,通过添加
将第一个元素(包含我们需要的所有信息的 person 对象)添加到字典中
它的 .Count 增加到 1(准备下一次添加)。你很容易就能看到
.Add .Count + 1 是浪费时间/精力:

  Dim dicPersons : Set dicPersons = CreateObject( "Scripting.Dictionary" )
  Dim aPersons   : aPersons       = Array( _
        Array( "Adam", #1/5/2001#, 1234.56 ) _
      , Array( "Eve" , #1/6/2001#, 6543.21 ) _
  )
  Dim aPerson
  For Each aPerson In aPersons
      dicPersons.Add dicPersons.Count, New cPerson.init( aPerson( 0 ), aPerson( 1 ), aPerson( 2 ) )
  Next
  Dim nPerson
  WScript.Echo "Adam & Eve"
  For Each nPerson In dicPersons
      WScript.Echo nPerson, ":", Join( dicPersons( nPerson ).Data(), " - " )
  Next    
  dicPersons.Remove 0 ' how do we know the key of Adam?
  WScript.Echo "Adam zaped"
  For Each nPerson In dicPersons
      WScript.Echo nPerson, ":", Join( dicPersons( nPerson ).Data(), " - " )
  Next    
  WScript.Echo "Trying to add Caine"
 On Error Resume Next 
  dicPersons.Add dicPersons.Count, New cPerson.init( "Caine", Date(), 0.33 )  
  WScript.Echo Err.Description
 On Error GoTo 0

输出

Adam & Eve
0 : 0 - Adam - 1/5/2001 - 1234.56
1 : 1 - Eve - 1/6/2001 - 6543.21
Adam zaped
1 : 1 - Eve - 1/6/2001 - 6543.21
Trying to add Caine
This key is already associated with an element of this collection

显示了为什么具有基于 .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:

Dim g_nPersonId : g_nPersonId = -1
Class cPerson
  Private m_nId
  Private m_sName
  Private m_dtBirth
  Private m_dWage
  Public Function init( sName, dtBirth, dWage )
    Set init    = Me
    g_nPersonId = g_nPersonId + 1
    m_nId       = g_nPersonId
    Name        = sName
    Birth       = dtBirth
    Wage        = dWage
  End Function  
  Public Property Let Name(  sName   ) : m_sName   = sName     : End Property
  Public Property Let Birth( dtBirth ) : m_dtBirth = dtBirth   : End Property
  Public Property Let Wage(  dWage   ) : m_dWage   = dWage     : End Property
  Public Property Get Id()             : Id        = m_nId     : End Property
  Public Property Get Name()           : Name      = m_sName   : End Property
  Public Property Get Birth()          : m_dtBirth = m_dtBirth : End Property
  Public Property Get Wage()           : m_dWage   = m_dWage   : End Property
  Public Function Data()
    Data = Array( m_nId, m_sName, m_dtBirth, m_dWage )
  End Function  
End Class ' cPerson

[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:

  Dim oAdam : Set oAdam = New cPerson.init( "Adam", #1/5/2001#, 1234.56 )
  Dim oEve  : Set oEve  = New cPerson.init( "Eve" , #1/6/2001#, 6543.21 )
  Dim oPerson
  For Each oPerson In Array( oAdam, oEve )
      WScript.Echo Join( oPerson.Data(), " - " )
  Next    

output:

0 - Adam - 1/5/2001 - 1234.56
1 - Eve - 1/6/2001 - 6543.21

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:

  Dim dicPersons : Set dicPersons = CreateObject( "Scripting.Dictionary" )
  Dim aPersons   : aPersons       = Array( _
        Array( "Adam", #1/5/2001#, 1234.56 ) _
      , Array( "Eve" , #1/6/2001#, 6543.21 ) _
  )
  Dim aPerson
  For Each aPerson In aPersons
      dicPersons.Add dicPersons.Count, New cPerson.init( aPerson( 0 ), aPerson( 1 ), aPerson( 2 ) )
  Next
  Dim nPerson
  WScript.Echo "Adam & Eve"
  For Each nPerson In dicPersons
      WScript.Echo nPerson, ":", Join( dicPersons( nPerson ).Data(), " - " )
  Next    
  dicPersons.Remove 0 ' how do we know the key of Adam?
  WScript.Echo "Adam zaped"
  For Each nPerson In dicPersons
      WScript.Echo nPerson, ":", Join( dicPersons( nPerson ).Data(), " - " )
  Next    
  WScript.Echo "Trying to add Caine"
 On Error Resume Next 
  dicPersons.Add dicPersons.Count, New cPerson.init( "Caine", Date(), 0.33 )  
  WScript.Echo Err.Description
 On Error GoTo 0

The output

Adam & Eve
0 : 0 - Adam - 1/5/2001 - 1234.56
1 : 1 - Eve - 1/6/2001 - 6543.21
Adam zaped
1 : 1 - Eve - 1/6/2001 - 6543.21
Trying to add Caine
This key is already associated with an element of this collection

shows why dictionaries with numerical indices based on .Count are no solution
for the task: Maintain a collection of persons.

动听の歌 2024-12-05 09:04:28

在本例中,变量 list 包含一个 Scripting.Dictionary

当您在 Scripting.Dictionary 上使用 VBScript For Each 构造时,会返回字典中的每个。因此,在这种情况下,For Each 将在每次迭代时循环将每个键放入变量 e 中。

Line:-

 Set le = list(e)

现在使用此循环的键来查找与该键关联的字典中的值。在这种情况下,该值是一个具有属性的对象。

因此,字典可用于使用其键快速直接地查找“表”中的“行”。此外,正如您发布的代码所示,您可以枚举每个键并查找每个值以“扫描”整个“表”。

In this case the variable list holds a Scripting.Dictionary.

When you use the VBScript For Each construct on Scripting.Dictionary each key in the dictionary is returned. So in this case the For Each will loop place each key in the variable e on each iteration.

The Line:-

 Set le = list(e)

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".

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