Use a multidimensional array instead of two separate ones?

A lot of stupid questions lately, but I would appreciate some material on this. I have a line that comes from an INI file. It looks like Firstname=FIRSTNAME . It is basically an array with many of them. I want to smash them, but keep them both. So, I managed to put Firstname in my own array and Firstname in it own. But then my colleague said: "Why don't you just use a multidimensional array?" And that made me think, putting Firstname at 0 and Firstname at 1. But how to do it?

This is my code right now:

  For iTeller = 0 To UBound(arrIniName) If Not arrIniName(iTeller) = "" Then arrIniName(iTeller) = Split(arrIniName(iTeller), "=")(0) End If Next For iTeller = 0 To UBound(arrIniValue) If Not arrIniValue(iTeller) = "" Then arrIniValue(iTeller) = Split(arrIniValue(iTeller), "=")(1) End If Next 

Both the arrIniName and arrIniValues ​​attributes consist of the same array to start with. Which looks like this:

 arrIniName(0) "Fistname=FIRSTNAME" arrIniName(1) "Lastname=LASTNAME" arrIniName(2) "Initials=INITIALS" 

Therefore, I basically split each into its own arrays as I am doing it now. But perhaps it would be better to place them in a multidimensional array? Because then I will have only one array to control, and also pull this array through a For Each loop.

Edit: I ended this up where Values is an array

 For Each s In Values Dim strName, strValue s = Split(s, "=") strName = s(0) strValue = s(1) 'Run function strName, strValue Next 
+4
source share
1 answer

The ideal solution looks like a Dictionary (a data structure that contains Key / Value pairs - exactly what you have in your INI file).

A multidimensional array is not required here, since you have only 2 dimensions (key and value). Arrays are usually harder to work than a dictionary, because they are hard to change, so you need to know how many items you have.

Therefore, I would suggest the following code:

 Dim dict As Dictionary Set dict = new Dictionary Dim key as String Dim value as String For iTeller = 0 To UBound(arrIniValue) If Not arrIniValue(iTeller) = "" Then key = Split(arrIniValue(iTeller), "=")(0) value = Split(arrIniValue(iTeller), "=")(1) dict.Add(key, value) End If Next 

However, if you want to use a multidimensional array, then this will do the following:

 ' Declare a 2-dimensional array, of dimensions "n by 2". Dim results(UBound(arrIniValue), 2) As String For iTeller = 0 To UBound(arrIniValue) If Not arrIniValue(iTeller) = "" Then key = Split(arrIniValue(iTeller), "=")(0) value = Split(arrIniValue(iTeller), "=")(1) results(iTeller, 0) = key results(iTeller, 1) = value End If Next 
+4
source

Source: https://habr.com/ru/post/1345837/


All Articles