我使用的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
答案 0 :(得分:5)
如果要逐行声明数组值以允许注释,则有两个选项。
如果您有固定数量的数组项,则可以定义数组,然后填充每个元素。
chicken soup
turkey
mash potatoes
yams
stuffing
输出:
ReDim
如果您需要动态声明,可以使用Preserve
。 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
输出:
foo.myFunctions() // works in browser console, but foo can't be used in typescript file
答案 1 :(得分:3)
只需在每行的末尾添加一个下划线,如下所示:
recipes = Array("chicken soup",_
"turkey",_
"mash potatoes",_
"yams",_
"stuffing")
注意:但即使在这种情况下,您也无法为每一行添加评论。
答案 2 :(得分:2)
这是我最终使用的解决方案,感谢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
答案 3 :(得分:1)
因为:
>> 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
将是一个糟糕的评论。