1

我正在尝试编写一个 VBScript,它将读取一个文本文件并构建一个包含列表的字典。让我用一个例子来解释:

输入文件将是一个简单的文本文件,如下所示:

Male,Peter
Male,Chris
Male,Stewie
Male,Brian
Female,Lois
Female,Meg

当我运行我的脚本时,我想要一个字典,第一列作为键,第二列作为值

{'Male':['Peter,Chris,Stewie,Brian']}
{'Female':['Lois,Meg']}

VBScript 中缺少动态数组或列表使这变得非常痛苦。有什么建议我可以解决这个问题吗?

干杯

4

4 回答 4

4

VBScript 可以使用System.Collections.ArrayList.NET 框架提供的类。

Set d = CreateObject("Scripting.Dictionary")
d.Add "Male", CreateObject("System.Collections.ArrayList")
d.Add "Female", CreateObject("System.Collections.ArrayList")

d("Male").Add "Peter"
d("Male").Add "Chris"
'...

d("Female").Add "Lois"
d("Female").Add "Meg"
'...

要处理输入文件,请查看@Rich 提供的代码。

于 2013-01-23T19:21:50.013 回答
1

只是说,我不与已发布的回购点答案竞争;)如果您可以将我的帖子转换为评论,请随时这样做。

我喜欢 Ansgar 的想法 (+1),因为它基于单个 Dictionary,在我看来,这足以轻松找回里面存储的内容。

.Exists的需要可能会在两种情况下使用 - (a) 如果我们不知道我们有多少种性别,以及 (b) 如果我们不知道它们的样子(发音)。其余的和Ansgar的想法类似。

Option Explicit

Const cGender = 0
Const cName = 1

Dim sGender, sName, sLine
Dim oFSO, oFile, oDict
Dim arrLine

Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oDict = CreateObject("Scripting.Dictionary")
Set oFile = oFSO.OpenTextFile("persons_list.txt")

Do Until oFile.AtEndOfStream
    sLine = oFile.ReadLine
    If Len(sLine) Then
        arrLine = Split(sLine, ",")
        sGender = arrLine(cGender)
        sName = arrLine(cName)

        If Not oDict.Exists(sGender) Then
            oDict.Add sGender, CreateObject("System.Collections.ArrayList")
        End If

        oDict(sGender).Add sName
    End If
Loop

oFile.Close
Set oFile = Nothing
Set oFSO = Nothing

WScript.Echo "Genders:" & oDict.Count, vbNewLine & Join(oDict.Keys)

Dim sKey
For Each sKey In oDict
    WScript.Echo sKey, oDict(sKey).Count, vbNewLine & Join(oDict(sKey).ToArray())
Next
于 2013-01-24T13:39:08.893 回答
0

我对字典对象不太熟悉,但这可能就足够了吗?

Set oFso = CreateObject("Scripting.FileSystemObject")
Set oDictionary = CreateObject("Scripting.Dictionary")

Const cGender = 0
Const cName = 1

Set oFile = oFso.OpenTextFile ("yourfile.txt", 1) 
Do Until oFile.AtEndOfStream
  sLine = oFile.Readline
  If sLine <> "" Then
    arrLine = split(sLine,",")
    oDictionary.Add arrLine(cGender,0), arrLine(cName,0) 'hope i got these indexes the right way round
    'or
    oDictionary.Add arrLine(cGender), arrLine(cName) 'if its one dimentional
  End If
Loop
于 2013-01-23T12:40:38.387 回答
0

由于@Rich 方法是错误的 (-1) - 你不能 .Add 一个键两次,如果可以的话,名称将被覆盖,而不是附加 - 而 Ansgar 的好主意 (+1) 在没有提示的情况下并不是真正的生产就绪wrt它的实际用途:

  Const cGender = 0
  Const cName = 1

  Dim oFS    : Set oFS    = CreateObject("Scripting.FileSystemObject")
  Dim dicDic : Set dicDic = CreateObject("Scripting.Dictionary")
  Dim dicAl  : Set dicAl  = CreateObject("Scripting.Dictionary")
  Dim dicCnt : Set dicCnt = CreateObject("Scripting.Dictionary")

  Dim oFile, sLine, arrLine
  Set oFile = oFS.OpenTextFile ("so14479571.txt")
  Do Until oFile.AtEndOfStream
     sLine = oFile.Readline()
     If sLine <> "" Then
        arrLine = Split(sLine,",")
        dicCnt(arrLine(cGender)) = dicCnt(arrLine(cGender)) + 1

        If Not dicDic.Exists(arrLine(cGender)) Then
           Set dicDic(arrLine(cGender)) = CreateObject("Scripting.Dictionary")
        End If
        dicDic(arrLine(cGender))(arrLine(cName)) = Empty

        If Not dicAl.Exists(arrLine(cGender)) Then
           Set dicAl(arrLine(cGender)) = CreateObject("System.Collections.ArrayList")
        End If
        dicAl(arrLine(cGender)).Add arrLine(cName)
     End If
  Loop
  Dim sKey, sKey2
  WScript.Echo "genders:"
  For Each sKey In dicCnt
      WScript.Echo "", sKey, dicCnt(sKey)
  Next
  WScript.Echo "dic:"
  For Each sKey In dicDic
      WScript.Echo "", sKey
      For Each sKey2 In dicDic(sKey)
          WScript.Echo " ", sKey2
      Next
  Next
  WScript.Echo "AL:"
  For Each sKey In dicAl
      WScript.Echo "", sKey & ":", Join(dicAl(sKey).ToArray())
  Next

输出:

genders:
 Male 4
 Female 2
dic:
 Male
  Peter
  Chris
  Stewie
  Brian
 Female
  Lois
  Meg
AL:
 Male: Peter Chris Stewie Brian
 Female: Lois Meg

脚本应显示:

  1. 如何在没有 .Exists 的情况下使用可以自动激活的“简单”值的赋值(这里是一个数字)
  2. 您需要 .Exists 检查 Arrays、ArrayLists 或 Dictionaries 等值
于 2013-01-23T19:57:21.253 回答