How do I create a VBScript multiline array?

Viewed 2602

I have a VBScript file that I use that has a lot of values in an array.

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

What is the proper way to declare this array over multiple lines, something similar to this:

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

That way I can write comments on each line (or is this correct?):

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 Answers

Just add an underscore at the end of each line as shown below:

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

Note: But even in this case, YOU CANNOT ADD COMMENTS FOR EACH LINE.

You have two options, if you want to declare array values line by line to allow comments.

  1. If you have a fixed number of array items, you can define the array then populate each element.

    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
    

    Output:

    chicken soup
    turkey
    mash potatoes
    yams
    stuffing
    
  2. If you need to declare dynamically you can use ReDim. The Preserve keyword tells ReDim to not empty the array when resizing the dimension.

    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
    

    Output:

    chicken soup
    turkey
    mash potatoes
    yams
    stuffing
    

Useful Links

This is the solution I ended up using, thanks to Lankymart for suggesting ReDim, it works exactly how I want. I can have a list of items that get added to the array that can be commented out entirely or rearranged. For my purpose the code is used in a small utility and speed is of absolutely no concern.

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

Because this:

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

would be a bad comment.

Related