Multiple strings in multidimensional array vbscript

后端 未结 3 1209
面向向阳花
面向向阳花 2021-01-06 13:18

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

相关标签:
3条回答
  • 2021-01-06 13:38

    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
    
    0 讨论(0)
  • 2021-01-06 13:47

    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"
    
    0 讨论(0)
  • 2021-01-06 13:50

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

    0 讨论(0)
提交回复
热议问题