Storing stuff
#13
That said, there is a really nice way to load and save line record files of variable length strings using Split and Join$ routines.

To load:
1. Open file for Binary
2. Read it's size and make a get buffer
buff$ = space$(lof(filenum))
3. Get fileNum,  , buff$
4. close
5. Split buff$, delimiter$, myArr$()

MyArr$ now contains all the variable length file lines in an array for processing data in code.

but now I have to explain delimiter$ which is Chr$(13) + Chr$(10) for bas and old txt file but some modern txt files now just use chr$(10) or you could just use Chr$(10) and still read it from WP. They separate one "record" or variable length string from another.

and MyArr$() needs to be dynamic ie started with REDIM not Dim for the particular Split sub I have in mind:
(Dynamic means it can be resized as needed with REDIM _Preserve or just REDIM to clear last array and start with new with same variable name.

Code: (Select All)
' note: I buggered this twice now, FOR base 1 array REDIM MyArray (1 to 1) AS ... the (1 to 1) is not same as (1) which was the Blunder!!!
'notes: REDIM the array(0) to be loaded before calling Split '<<<< IMPORTANT dynamic array and empty, can use any lbound though
'This SUB will take a given N delimited string, and delimiter$ and create an array of N+1 strings using the LBOUND of the given dynamic array to load.
'notes: the loadMeArray() needs to be dynamic string array and will not change the LBOUND of the array it is given.  rev 2019-08-27
Sub Split (SplitMeString As String, delim As String, loadMeArray() As String)
    Dim curpos As Long, arrpos As Long, LD As Long, dpos As Long 'fix use the Lbound the array already has
    curpos = 1: arrpos = LBound(loadMeArray): LD = Len(delim)
    dpos = InStr(curpos, SplitMeString, delim)
    Do Until dpos = 0
        loadMeArray(arrpos) = Mid$(SplitMeString, curpos, dpos - curpos)
        arrpos = arrpos + 1
        If arrpos > UBound(loadMeArray) Then ReDim _Preserve loadMeArray(LBound(loadMeArray) To UBound(loadMeArray) + 1000) As String
        curpos = dpos + LD
        dpos = InStr(curpos, SplitMeString, delim)
    Loop
    loadMeArray(arrpos) = Mid$(SplitMeString, curpos)
    ReDim _Preserve loadMeArray(LBound(loadMeArray) To arrpos) As String 'get the ubound correct
End Sub

Function Join$ (arr() As String, delimiter$)
    Dim i As Long, b$
    For i = LBound(arr) To UBound(arr)
        If i = LBound(arr) Then b$ = arr(LBound(arr)) Else b$ = b$ + delimiter$ + arr(i)
    Next
    Join$ = b$
End Function

Where there is splitting there is complementary Join$ that converts an array into a really long string. That's when you want to file a variable string array back in a single Put for binary or easiest Open for Output and print filenum, the Join$'d string.

Then to file data from MyArr$()
buff$ = Join$(MyArr$(), Chr$(13) + Chr$(10))

and of course use
delimiter$ = Chr$(13) + Chr$(10)
because you don't want to do that + for every line!

So save a variable string array to file:
Open file for output as #filenum
Print #filenum, Join$(myArr$(), delimiter$)
Close

But these methods aren't felt in speed until in 1000's of strings.

In summary, it's nice if you have Split and Join$ in your arsenal of tools and know how to use them and, you have a file with maybe 1000's of lines to load or save.
b = b + ...
Reply


Messages In This Thread
Storing stuff - by NasaCow - 12-14-2022, 10:42 AM
RE: Storing stuff - by mdijkens - 12-14-2022, 01:26 PM
RE: Storing stuff - by SMcNeill - 12-14-2022, 02:17 PM
RE: Storing stuff - by NasaCow - 12-14-2022, 02:31 PM
RE: Storing stuff - by SpriggsySpriggs - 12-14-2022, 04:40 PM
RE: Storing stuff - by Pete - 12-14-2022, 08:04 PM
RE: Storing stuff - by TempodiBasic - 12-14-2022, 11:39 PM
RE: Storing stuff - by NasaCow - 12-15-2022, 06:05 AM
RE: Storing stuff - by Pete - 12-15-2022, 11:51 AM
RE: Storing stuff - by mdijkens - 12-15-2022, 12:26 PM
RE: Storing stuff - by SpriggsySpriggs - 12-15-2022, 02:36 PM
RE: Storing stuff - by bplus - 12-15-2022, 02:57 PM
RE: Storing stuff - by bplus - 12-15-2022, 04:08 PM



Users browsing this thread: 1 Guest(s)