3

I am trying to add multiple strings into a multidimensional array in VBScript. I hope I can explain it in a short way: Every string contains some data separated by commas. Now I would like to add all these data into an array, one dimension for every string.

For example

Dates = "12.02.2016, 13.08.2017, 19.05.2018"
Temperatures = "23.1, 24.9, 75.3"
Humidity = "26, 29, 95"

It is no Problem to get every String into an one dimensional array by using

AmbientConditionsArray = Split(Dates, ", ")

But I really have no idea to get it into a two dimensional array like

AmbientConditionsArray(0,0) = Date1
AmbientConditionsArray(0,1) = Temperature1
AmbientConditionsArray(0,2) = Humidity1
AmbientConditionsArray(1,0) = Date2
AmbientConditionsArray(1,1) = Temperature2
AmbientConditionsArray(1,2) = Humidity2

and so on.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
C.Schwan
  • 33
  • 1
  • 4

3 Answers3

3

While you can use a multidimensional array, it will be comparatively cumbersome.

How about simply using three separate regular arrays?

Dates = Split("12.02.2016, 13.08.2017, 19.05.2018", ", ")
Temperatures = Split("23.1, 24.9, 75.3", ", ")
Humidity = Split("26, 29, 95", ", ")

to work with them as a block of values I would use a dictionary.

Dim AmbientConditions
Set AmbientConditions = CreateObject("Scripting.Dictionary")

AmbientConditions.Add "Dates", Split("12.02.2016, 13.08.2017, 19.05.2018", ", ")
AmbientConditions.Add "Temperatures", Split("23.1, 24.9, 75.3", ", ")
AmbientConditions.Add "Humidity", Split("26, 29, 95", ", ")

Later you can access individual values in a readable manner:

MsgBox AmbientConditions("Humidity")(1)
' -> shows "29"
Tomalak
  • 332,285
  • 67
  • 532
  • 628
2

@Tomalak's answer is a neat way of doing it but in case you did want a native Multi-Dimensional Array approach closer to your original request you would use a Dynamic Array, like this.

Option Explicit

'Named constants for the three dimensions
Const COND_DATE = 0
Const COND_TEMPERATURE = 1
Const COND_HUMIDITY = 2

Dim Dates, Temperatures, Humidity, i
Dim AmbientConditions()

Dates = Split("12.02.2016, 13.08.2017, 19.05.2018", ", ")
Temperatures = Split("23.1, 24.9, 75.3", ", ")
Humidity = Split("26, 29, 95", ", ")

ReDim AmbientConditions(2, UBound(Dates))

For i = 0 to Ubound(Dates)
    AmbientConditions(COND_DATE, i) = Dates(i)
    AmbientConditions(COND_TEMPERATURE, i) = Temperatures(i)
    AmbientConditions(COND_HUMIDITY, i) = Humidity(i)
Next

For i = 0 To UBound(AmbientConditions, 2)
    WScript.Echo AmbientConditions(COND_DATE, i)
    WScript.Echo AmbientConditions(COND_TEMPERATURE, i)
    WScript.Echo AmbientConditions(COND_HUMIDITY, i) & vbCrLf
Next

Output:

12.02.2016
23.1
26

13.08.2017
24.9
29

19.05.2018
75.3
95

Also followed @Tomalak's suggestion to use Named Constants for the various dimensions.

Community
  • 1
  • 1
user692942
  • 16,398
  • 7
  • 76
  • 175
0

Taking form your code here is what could be done; this is a true multi-dimensional array. [Code was edited due to slight oversight on my part]

Dates = Split("12.02.2016, 13.08.2017, 19.05.2018", ", ")
Temperatures = Split("23.1, 24.9, 75.3", ", ")
Humidity = Split("26, 29, 95", ", ")

Dim AmbientConditionsArray()
ReDim AmbientConditionsArray(2,UBound(Dates))
For i = 0 TO UBound(Dates)
    AmbientConditionsArray(0,I) = Dates(I)
    AmbientConditionsArray(0,I) = Temperatures(I)
    AmbientConditionsArray(0,I) = Humidity(I)
Next

Now all your data is neatly stored in a single multi-dimensional array.

This second model is close to the first answer, but I think it meets your stated need as well. [It uses nested arrays to meet the objective of a hybrid multi-dimensional array]

Dates = Split("12.02.2016, 13.08.2017, 19.05.2018", ", ")
Temperatures = Split("23.1, 24.9, 75.3", ", ")
Humidity = Split("26, 29, 95", ", ")

Dim AmbientConditions

AmbientConditions = Array(Dates, Temperatures, Humidity)

For I = 0 To UBound(Dates)
    Debug.Print AmbientConditions(0)(I), AmbientConditions(1)(I), AmbientConditions(2)(I)
Next
CoveGeek
  • 425
  • 3
  • 10
  • 2
    In that case I would recommend the use of `Const COND_DATE = 0` (etc) to increase readability of the code: `AmbientConditions(COND_DATE)(i)`. The biggest issue I have with using multidimensional arrays (apart from their inflexibility once defined) is that they produce code that's hard to read with all those numerical indexes. – Tomalak Feb 12 '16 at 14:48
  • This isn't a *"multi-dimensional array"* it is a single dimensional array containing nested single dimensional arrays. In affect a copy of @Tomalak's approach using an `Array()` instead of `Scripting.Dictionary`. – user692942 Feb 12 '16 at 15:04
  • @Lankymart I supplied the hybrid approach after the 'true' multi-dimensional approach. You should see that either way you want to slice it is there.@Tomalak I like your insight, that makes perfect sense :) – CoveGeek Feb 12 '16 at 15:10
  • I'm not sure you can do that in VBScript...be right back going to test it. – user692942 Feb 12 '16 at 15:12
  • I tested it in excel VBA, but the 1st example will work for sure in vbscript. – CoveGeek Feb 12 '16 at 15:13
  • @CoveGeek No it won't. Second works if you get rid of `Debug.Print`. This question isn't about VBA. – user692942 Feb 12 '16 at 15:17
  • @Lankymart I updated the 1st code block to work without error in vbscript. Thank you for the critique. – CoveGeek Feb 12 '16 at 15:24
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/103315/discussion-between-covegeek-and-lankymart). – CoveGeek Feb 12 '16 at 15:26