NthField and Split

The documentation for NthField states:
Using NthField in a loop to extract fields from a string is inefficient. You should use Split for this purpose.

So far, so good, I used Split:

But now, I need to extract the data from the Array:

theArray = Split(theText, “}”)

Worst: each entry holds two parts, so I still have to use NthField and a Loop to place the data correctly.

Current design (fake code):

Do
  AddRow Field1 // Using NthField
  RowTag() Field2 // Using NthField
Loop 'till the End

What is your advice ?

Split it again? :wink:

Var strBigSplit(-1) As String = Split...bigchunkofstrings
Var strSmallSplit(1) As String

While strBigSplit.RowCount > 0
  strSmallSplit = Split...strBigSplit(0)
  AddRow strSmallSplit(0) // Splitted
  RowTag() strSmallSplit(1) // :-D
  strBigSplit.RemoveRowAt(0)
Wend

Yes. This escapes my mind :weary:.

The documentation advice is really good when we face ```
BigChunkOfStrings

Else, using NthField in loop works fine and fast: on 60KB file, it is nearly instantly displayed (with a For Next loop).

The problem with NthField in a loop is that every time you call it you are starting fresh from the string and splitting it up, over and over again.

As for Sascha’s suggestion I would think it would be better to not remove the elements from the array as you work through the array. It’s a memory manipulation that you don’t need to do. Surely the following would be faster:

Var strBigSplit(-1) As String = Split...bigchunkofstrings
Var strSmallSplit(1) As String

For Each ThisString as String in strBigSplit
   strSmallSplit = ThisString.Split( inner_delimiter )
   AddRow strSmallSplit(0) // Splitted
   RowTag() strSmallSplit(1) // :-D
Next

Not changing the size of the array in every loop surely must be an advantage.

1 Like

@Ian_Kennedy is 100% correct. :slight_smile:

I am used to working with very large arrays and it is not uncommon at my site for memory requirements to be more important than real-time processing. :wink: