Multiple strings in multidimensional array vbscrip

2020-03-26 06:02发布

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.

3条回答
贼婆χ
2楼-- · 2020-03-26 06:20

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

查看更多
Explosion°爆炸
3楼-- · 2020-03-26 06:27

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"
查看更多
▲ chillily
4楼-- · 2020-03-26 06:44

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
查看更多
登录 后发表回答