I want to have a Jagged Array with two layers: 1) being a number, 2) being an Array of Strings so I can easily reference an array based off an index value. The point of this is to get the content of a text file so that each index in the jagged array is a row of the text file (layer 1 = index, layer 2 = row). These rows are populated by strings of course.
Without any adjustments, the text file has unwanted space in between strings in each row. I want the Array of strings to not include any of the wasted space ("Hello how are you "
--> ["hello","how","are","you"]
).
I do this though the Trim
function and the Split
function. Trim
removes all but the delimiting spaces; Split
produces the Array of Strings I want for that line. My issue is getting the array into the Jagged Array as well as creating an array without knowing its length ahead of time, as I have not Split
the row of text yet.
Below is my code. When I use a variant instead of a String as my second layer I get another error which I cant seem to solve. Note: the string array which contains the text file information is tempString()
*UPDATED CODE:
so you can test this, use tempString = (" test tempstring ", "", " test test test", " "," test ", "")
Private Sub createGCStruct(ByRef tempString() As String)
' note many parameters are not included.
' also, this would be a function producing a structure, but for now I just need this to properly create a
' jagged array.
' set variables
Dim tempString2() As String
ReDim tempString2(UBound(tempString()))
Dim j As Integer
Dim jaggArray() As Variant '*****outer layer of the jagged array*****
ReDim jaggArray(UBound(tempString()) + 1)
Dim stringST() As String '*****inner layer of the jagged array*****
Dim tempString4() As String
' set initial values of structure
' ...more code...
' capture structure information from textfile array
' A) remove unnecessary spaces from existing Array
For j = LBound(tempString()) To UBound(tempString())
' check to see if line is zero length string
If tempString(j) = "" Then
' what you don't see are my commented out, futile attempts at
' solving this problem; just know that they exist
Erase stringST
stringST = tempString(j)
jaggArray(j + 1) = stringST
' trim excesive spacing
Else
tempString2(j) = Trim(tempString(j))
Erase stringST
stringST = Split(tempString2(j), " ")
jaggArray(j + 1) = stringST
End If
Next j
'Below is me testing to see if this works'
tempString4 = jaggArray(1)
MsgBox tempString4(0), vbExclamation, "test"
' B) Add sections from array to structure
'... more code...
End Sub