Recursive Split Function using One Array

J

jamiedora

Hello,

I wanted to ask if there was a method for recusevly applying the split
function to a single array. Currently, I place all values into the
array 'Lines' then plug these values into the array 'Lines1' (this loop
is denoted by '**********).

Because I would like to optimize this procedure (this is a very large
text file, so I had to break the string into chunks due to memory
issues), can I recursivly add elements to the array 'Lines' without
having to create a new array 'Lines1'?

Thanks,

'-- Code --


FN = FreeFile
Index1 = 0
Index0 = 0
VarChunkPos = 1
Open FileName For Binary As FN
VarChunk = 100000
ChunkLimit = LOF(FN)
TotalFile = Space(VarChunk)
VarChunkPos = 1
Index0 = 0
Do
TotalFile = Space(VarChunk)
Get #FN, VarChunkPos, TotalFile 'Read entire file into the TotalFile
string variable
Lines = Split(TotalFile, vbLf) 'Split the file using the Line Feed
character as delimiter
ReDim Preserve Lines(UBound(Lines))
TotalFile = ""
Index0 = -1 'Reset counter

Do '**********
Index0 = Index0 + 1
Index1 = Index1 + 1
If Index0 = 0 And Index1 <> 1 Then
Lines1(Index1 - 1) = Lines1(Index1 - 1) & Lines(Index0)
Else
ReDim Preserve Lines1(Index1)
Lines1(Index1) = Lines(Index0)
End If
Loop Until Index0 = UBound(Lines) '**********

If VarChunkPos > ChunkLimit Then
Exit Do
End If
VarChunkPos = VarChunk + VarChunkPos
Loop
 
K

Karl E. Peterson

I wanted to ask if there was a method for recusevly applying the split
function to a single array. Currently, I place all values into the
array 'Lines' then plug these values into the array 'Lines1' (this
loop is denoted by '**********).

Because I would like to optimize this procedure (this is a very large
text file, so I had to break the string into chunks due to memory
issues), can I recursivly add elements to the array 'Lines' without
having to create a new array 'Lines1'?

I gotta say, it's tough to tell from your example why exactly you're going
to all this trouble. Two optimization truths jump out at me, though.

#1) Concatenation is *expensive* -- avoid at any opportunity.
#2) Redim Preserve is *expensive* -- allocate blocks of elements (100s or
1000s, at a time), rather than individual elements.

Later... Karl
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top