3

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
Jieiku
  • 489
  • 4
  • 15

4 Answers4

7

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.

Gurmanjot Singh
  • 10,224
  • 2
  • 19
  • 43
  • yeah, I tried that earlier. I am trying to create an array where I can have comments for each entry. The only thing I can think of is to have some sort of list with comments, and then create the array from that list using a loop or something... I have not figured out a solution yet, but even if I have to do some weird round about way that would be fine. – Jieiku Nov 11 '17 at 07:11
  • @xekon Just declare the array, then define each value `recipes(0) = “chicken soup”`. That way you can add comments on each line. – user692942 Nov 11 '17 at 10:16
5

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

user692942
  • 16,398
  • 7
  • 76
  • 175
  • The solution of declaring the size beforehand and then declaring each element of the array directly works, however then its not easy to rearrange the list, like if I wanted to moves stuffing from recipes(4) to recipes(0) I would have to adjust all the indexes on the other lines, bumping them all down +1 Or if I wanted to comment out a line all together such as recipes(2) = "mash potatoes" then the array would be skipping over that value alltogether... – Jieiku Nov 11 '17 at 20:56
  • I am thinking the redim method might work if I create a helper function called AddItem() that way all I am doing is AddItem("yams") 'yummy yams this way I could rearrange the list or even comment out entire lines. A function like that should be doable in vbscript right? – Jieiku Nov 11 '17 at 21:04
  • 1
    @xekon There is no mention of any of that in your question, we are not mind readers. If you had said that, would have suggested not using an array and using a `Scripting.Dictionary` instead. – user692942 Nov 11 '17 at 21:54
  • 1
    I think I like the redim method better than dictionary because then they still have a numeric index. Going to try and make a helper function. Thanks for the help Lankymart, and yes I should have better described what I am trying to accomplish. will post the solution once i figure out the helper function :) – Jieiku Nov 12 '17 at 03:35
2

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
Jieiku
  • 489
  • 4
  • 15
  • This approach creates an array with an empty last element. `recipes` should start empty and be grown **before** assignment. – Ekkehard.Horner Nov 12 '17 at 10:15
  • I realize you should grow it first then assign, however what about element 0? should I just use an if statement? if UBound(recipes) == 0 then assign value, otherwise grow. Because we dont want to skip the 0 index either. I know using an if statement and redim before assignment would resolve, it but using the if statement seems messy. – Jieiku Nov 12 '17 at 11:14
  • I modified the solution to account for the empty last element, however I wonder if there would be a solution involving less lines of code. – Jieiku Nov 12 '17 at 11:25
  • You don't need the `If`, just declare it dynamic `Dim recipes()` then the `ReDim` will be the same for each. **Edit:** Just seen Ekkehard's answer, basically do that. :D – user692942 Nov 12 '17 at 19:43
1

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.

Ekkehard.Horner
  • 38,498
  • 2
  • 45
  • 96
  • A good example of how to use dynamic arrays. Only thing I will say is the use of `Array()` instead of `Dim aa()` means you are limited to a single dimension array, but then there is no need for a multi-dimension array in this question. Still would prefer the `Dim aa()` approach over `Array()`. – user692942 Nov 12 '17 at 19:47