0

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
CSAW
  • 114
  • 1
  • 3
  • 16
  • 1
    Please provide a [self-contained example](http://www.sscce.org/) that will allow us to reproduce the problem. Else it's really difficult to help you. In the meantime, a couple of tips: What you want is a `Function`, not a `Sub`, as in `Private Function createGCStruct(...,...,[get rid of the last ByRef argument]) As gcBStruct`. Also, the `Erase stringST` are useless and can be removed. – Jean-François Corbett Aug 13 '14 at 07:33
  • 1
    http://stackoverflow.com/questions/9435608/how-do-i-set-up-a-jagged-array-in-vba –  Aug 13 '14 at 07:34
  • 1
    Also see this article for a `Split` function that treats multiple successive delimiters as one: http://msdn.microsoft.com/en-us/library/aa155763%28office.10%29.aspx – Jean-François Corbett Aug 13 '14 at 07:41
  • Is it possible to use a collection or dictionary instead to house the string arrays? I think one of my main issues is properly utilizing `stringST` as a "temporary" string that will simply place the string array into the Jagged Array. I can't define the size of the `stringST` array or set the individual elements in the split string values of each row into `stringST`. Furthermore, since I reuse `stringST`, I want to ensure I clear all prior values. What should I do in this case? – CSAW Aug 13 '14 at 17:02
  • @Jean-FrançoisCorbett I updated the code, you should be able to produce the problem. I kept the `Erase StringST` (for now) to maintain the originality of the problem. Thanks! – CSAW Aug 13 '14 at 17:37

1 Answers1

2

I can see that the "Can't assign to array" error occurs in the part of your code where you attempt to deal with the Split function's weird bugfeature behaviour whereby splitting an empty string Split("") returns a String(0 to -1) array that is utterly unusable.

The "Can't assign to array" error is caused by this line:

stringST = tempString(j)

The thing on the left, stringST, is an array. The thing on the right, tempString(j), isn't. (It's one element from the tempString array.) You can't assign a non-array to an array, hence the error.

What you can do is define an array that contains a single element, an empty string:

Dim emptyStringArrayPlaceholder() As String
ReDim emptyStringArrayPlaceholder(0 To 0)

And then use that as placeholder for empty strings:

stringST = emptyStringArrayPlaceholder

Here's how I would clean up your code:

Private Sub createGCStruct(ByRef tempString() As String)

    Dim jaggArray() As Variant                '*****outer layer of the jagged array*****

    jaggArray = splitStringArrayElements(tempString)

    '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

where I make use of this function:

Private Function splitStringArrayElements(tempString() As String) As Variant()
    Dim j As Long
    Dim trimmedString As String
    Dim jaggArray() As Variant
    ReDim jaggArray(LBound(tempString()) To UBound(tempString()))
    Dim emptyStringArrayPlaceholder() As String
    ReDim emptyStringArrayPlaceholder(0 To 0)

    For j = LBound(tempString()) To UBound(tempString())
        trimmedString = Trim(tempString(j))
        If trimmedString = "" Then
            jaggArray(j) = emptyStringArrayPlaceholder
        Else
            jaggArray(j) = Split(trimmedString, " ")
        End If
    Next j

    splitStringElements = jaggArray
End Function
Jean-François Corbett
  • 37,420
  • 30
  • 139
  • 188
  • Great explanation Jean! – CSAW Aug 14 '14 at 18:06
  • One thing, and I'm not sure if this was because you misunderstood what I was aiming for, was that with your solution the String Arrays in JaggArray still contain repetitive spaces/blanks in between words. I solved this by using a different version of split: http://msdn.microsoft.com/en-us/library/aa155763%28office.10%29.aspx – CSAW Aug 14 '14 at 21:24
  • 1
    Yes, well, I did give you that very link in my comment to your question above. My answer addressed the issue you specified in your question: "*My issue is getting the array into the Jagged Array...*". – Jean-François Corbett Aug 15 '14 at 07:09
  • ^Lol, you did. Sorry about that mate. – CSAW Aug 15 '14 at 07:30