代码之家  ›  专栏  ›  技术社区  ›  Jieiku

如何创建VBScript多行数组?

  •  3
  • Jieiku  · 技术社区  · 7 年前

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

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

    在多行上声明此数组的正确方法是什么,类似于以下内容:

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

    这样我可以在每一行上写下评论(或者这是正确的吗?):

    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
    
    4 回复  |  直到 7 年前
        1
  •  6
  •   Gurmanjot Singh    7 年前

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

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

    注意:但即使在这种情况下, 不能为每行添加注释。

        2
  •  5
  •   user692942    7 年前

    如果要逐行声明数组值以允许注释,有两个选项。

    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. ReDim . 这个 Preserve 关键字说明 重新定义 调整维度大小时不清空数组。

      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
      

      输出:

      鸡汤
      土耳其
      土豆泥
      山药
      填料
      

    有用的链接

        3
  •  2
  •   Jieiku    7 年前

    这是我最终使用的解决方案,多亏了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
    
        4
  •  1
  •   Ekkehard.Horner    7 年前

    因为:

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

    这将是一个糟糕的评论。