【问题标题】:How do I create a VBScript multiline array?如何创建 VBScript 多行数组?
【发布时间】:2017-11-11 03:30:01
【问题描述】:

我有一个我使用的 VBScript 文件,它的数组中有很多值。

recipes = Array("chicken soup","turkey","mash potatoes","yams","stuffing")

在多行中声明这个数组的正确方法是什么,类似于:

recipes = Array("chicken soup",
"turkey",
"mash potatoes",
"yams",
"stuffing")

这样我就可以在每一行上写 cmets(或者这样对吗?):

recipes = Array("chicken soup", 'broth, noodles, chicken
"turkey",         'YUMMY i love turkey
"mash potatoes",  'butter, sour cream, cook 20mins
"yams",           'dont forget the marshmallows
"stuffing")       'celery, jiffy cornbread, broth

【问题讨论】:

    标签: arrays vbscript comments multiline


    【解决方案1】:

    只需在每行末尾添加一个下划线,如下所示:

    recipes = Array("chicken soup",_
                    "turkey",_
                    "mash potatoes",_
                    "yams",_
                    "stuffing")
    

    注意:但即使在这种情况下,您也不能为每一行添加评论。

    【讨论】:

    • 是的,我之前尝试过。我正在尝试创建一个数组,我可以在其中为每个条目设置 cmets。我唯一能想到的就是有一些带有 cmets 的列表,然后使用循环或其他东西从该列表中创建数组......我还没有找到解决方案,但即使我必须做一些奇怪的回合就好了。
    • @xekon 只需声明数组,然后定义每个值recipes(0) = “chicken soup”。这样你就可以在每一行添加 cmets。
    【解决方案2】:

    如果您想逐行声明数组值以允许 cmets,您有两种选择。

    1. 如果您有固定数量的数组项,您可以定义数组然后填充每个元素。

      Dim receipes(4)
      Dim receipe
      
      receipes(0) = "chicken soup"  'Chicken Soup
      receipes(1) = "turkey"        'Turkey
      receipes(2) = "mash potatoes" 'Mash Potatoes
      receipes(3) = "yams"          'Yams
      receipes(4) = "stuffing"      'Stuffing
      
      For Each receipe In receipes
        WScript.Echo receipe
      Next
      

      输出:

      chicken soup
      turkey
      mash potatoes
      yams
      stuffing
      
    2. 如果需要动态声明,可以使用ReDimPreserve 关键字告诉 ReDim 在调整维度大小时不要清空数组。

      Dim receipe
      ReDim receipes(0)
      receipes(0) = "chicken soup"  'Chicken Soup
      ReDim Preserve receipes(1)
      receipes(1) = "turkey"        'Turkey
      ReDim Preserve receipes(2)
      receipes(2) = "mash potatoes" 'Mash Potatoes
      ReDim Preserve receipes(3)
      receipes(3) = "yams"          'Yams
      ReDim Preserve receipes(4)
      receipes(4) = "stuffing"      'Stuffing
      
      For Each receipe In receipes
        WScript.Echo receipe
      Next
      

      输出:

      chicken soup
      turkey
      mash potatoes
      yams
      stuffing
      

    有用的链接

    【讨论】:

    • 预先声明大小然后直接声明数组的每个元素的解决方案,但是重新排列列表并不容易,就像我想将填充物从食谱(4)移动到食谱(0) 我将不得不调整其他行上的所有索引,将它们全部降低 +1 或者如果我想将一行全部注释掉,例如 recipes(2) = "mash potato" 那么数组将跳过超过这个值...
    • 我认为 redim 方法可能会起作用,如果我创建一个名为 AddItem() 的辅助函数,我所做的就是 AddItem("yams") 'yummy yams 这样我可以重新排列列表或甚至注释掉整行。这样的函数在 vbscript 中应该是可行的吧?
    • @xekon 您的问题中没有提及任何内容,我们不是读心者。如果您这么说,建议不要使用数组,而是使用Scripting.Dictionary
    • 我想我比字典更喜欢 redim 方法,因为它们仍然有一个数字索引。打算尝试制作一个辅助功能。感谢 Lankymart 的帮助,是的,我应该更好地描述我想要完成的工作。一旦我弄清楚了帮助函数,就会发布解决方案:)
    【解决方案3】:

    这是我最终使用的解决方案,感谢 Lankymart 建议 ReDim,它完全符合我的要求。我可以有一个添加到数组中的项目列表,可以完全注释掉或重新排列。出于我的目的,该代码用于一个小型实用程序,速度完全无关紧要。

    Dim recipe, recipes
    ReDim recipes(0)
    
    Function AddRecipe(v)
      If recipes(0) = "" Then
        recipes(UBound(recipes)) = v
      Else
        ReDim Preserve recipes(UBound(recipes)+1)
        recipes(UBound(recipes)) = v
      End If
    End Function
    
    AddRecipe("Ham")            'Honey Glazed
    AddRecipe("turkey")         'YUMMY i love turkey
    AddRecipe("mash potatoes")  'butter, sour cream, cook 20mins
    AddRecipe("yams")           'dont forget the marshmallows
    AddRecipe("stuffing")       'celery, jiffy cornbread, broth
    
    For Each recipe In recipes
      WScript.Echo "value:" & recipe
    Next
    

    【讨论】:

    • 这种方法创建一个最后一个元素为空的数组。 recipes 应该从空开始并在之前分配。
    • 我知道你应该先增长它然后分配,但是元素 0 呢?我应该只使用 if 语句吗?如果 UBound(recipes) == 0 然后赋值,否则增长。因为我们也不想跳过 0 索引。我知道在分配之前使用 if 语句和 redim 会解决,但是使用 if 语句似乎很乱。
    • 我修改了解决方案以解决最后一个空元素,但是我想知道是否会有涉及更少代码行的解决方案。
    • 您不需要If,只需将其声明为动态Dim recipes(),然后ReDim 将是相同的。 编辑: 刚刚看到 Ekkehard 的回答,基本上就是这样做的。 :D
    【解决方案4】:

    因为:

    >> Sub Add2Array(a, v)
    >>   ReDim Preserve a(UBound(a) + 1)
    >>   a(UBound(a)) = v
    >> End Sub
    >> aa = Array()
    >> WScript.Echo 0, TypeName(aa), UBound(aa)
    >> Add2Array aa, "look, ma - one elm"
    >> WScript.Echo 1, TypeName(aa), UBound(aa), aa(0)
    >>
    0 Variant() -1
    1 Variant() 0 look, ma - one elm
    

    将是一个糟糕的评论。

    【讨论】:

    • 如何使用动态数组的一个很好的例子。我唯一要说的是使用Array() 而不是Dim aa() 意味着您仅限于一维数组,但是在这个问题中不需要多维数组。仍然更喜欢Dim aa() 方法而不是Array()
    猜你喜欢
    • 1970-01-01
    • 2011-06-03
    • 2013-11-22
    • 2011-11-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多