RAaddition

Additional random access function
This page contains some more functions for random access with arrays to facilitate some common tasks with database file I/O. These functions supplement the main functions described in Random access files with arrays. toc

function FindRecord(key$, fieldNo, startFrom, mode$)
This function returns the number of the first record where key was found, or 0 if there were no matches. The parameters are: If you want to search from the beginning of the file, this value must be 1. If, for example, you have found one match and want to continue with the rest of the file, this value should be (match+1). E.g, n = FindRecord("John", 2, 14, "") searches for an exact match to "John" in field #2, starting from record #14.
 * key$** - string to look for
 * fieldNo** - number of the field to search in. (This number, of course, must not be larger than the total number of fields in a record.)
 * startFrom** - number of the first record to start search with.
 * mode$** - string specifying the search mode. Modes are:
 * //"matchcase"//** or **//""//** - find exact match, case sensitive search
 * //"nomatchcase"//** - find exact match, not case sensitive search
 * //"partial"//** or **//"partial matchcase"//** - find partial match (key$ is a substring of a field), case sensitive
 * //"partial nomatchcase"//** - same as previous, except not case sensitive

Note: this function calls the GetRecord function, which must be included. code format="vb" function FindRecord(key$, fieldNo, startFrom, mode$) RecordCount = lof(#randomarray)/FieldLength(FieldLength(0)+1) select case lower$(mode$) case "nomatchcase" for i = startFrom to RecordCount a = GetRecord(i) if lower$(RecordField$(fieldNo)) = lower$(key$) then FindRecord = i                   exit for end if           next i        case "partial nomatchcase" for i = startFrom to RecordCount a = GetRecord(i) if instr(lower$(RecordField$(fieldNo)), lower$(key$)) > 0 then FindRecord = i                   exit for end if           next i        case "partial matchcase", "partial" for i = startFrom to RecordCount a = GetRecord(i) if instr(RecordField$(fieldNo), key$) > 0 then FindRecord = i                   exit for end if           next i        case else ' = "matchcase" for i = startFrom to RecordCount a = GetRecord(i) if RecordField$(fieldNo) = key$ then FindRecord = i : exit for next i   end select end function code

function GetField$(recordNo, fieldNo)
This function returns a string with a single field from record number //recordNo//. Unlike other functions it does not perform error checking. code format="vb" function GetField$(recordNo, fieldNo) i = 1 while i < fieldNo size = size+FieldLength(i) i = i+1 wend pos = (recordNo-1)*FieldLength(FieldLength(0)+1)+size seek #randomarray, pos GetField$ = input$(#randomarray, FieldLength(fieldNo)) GetField$ = trim$(GetField$) end function code

function PutField(putWhat$, recordNo, fieldNo)
This function is for saving (modifying) the contents of one field only. Field number //fieldNo// of record //recordNo// is overwritten with the contents of //putWhat$//. The string is is padded or truncated as needed. The function returns 1 for success or 0 for failure. code format="vb" function PutField(putWhat$, recordNo, fieldNo) i = 1 while i < fieldNo size = size+FieldLength(i) i = i+1 wend pos = (recordNo-1)*FieldLength(FieldLength(0)+1)+size if pos >= 0 and pos <= lof(#randomarray) then PutField = 1 putWhat$ = left$(putWhat$+space$(FieldLength(fieldNo)), FieldLength(fieldNo)) seek #randomarray, pos print #randomarray, putWhat$ end if end function code

function EraseRecord(no)
This function overwrites record number //no// with spaces, thus erasing it's contents, and returns 1 for success or 0 for failure. Note: it does not remove the record from the disk, therefore a call to this function //does not reduce// the total number of records in the file. code format="vb" function EraseRecord(no) pos = (no-1)*FieldLength(FieldLength(0)+1) if pos >= 0 and pos <= lof(#randomarray) then EraseRecord = 1 seek #randomarray, pos print #randomarray, space$(FieldLength(FieldLength(0)+1)) end if end function code

function FindEmptyRecord
This function returns the number of the first empty record in the file, or 0 if there are no empty records. Note: if the database application provides means to delete records, then over time the file may contain several empty records. To conserve disk space in this case it would be recommended to add a new record to the end of the file only if there are no empty records in the file, otherwise those unused spaces could be overwritten instead. code format="vb" function FindEmptyRecord size = FieldLength(FieldLength(0)+1) RecordCount = lof(#randomarray)/size for i = 1 to RecordCount seek #randomarray, pos s$ = input$(#randomarray, size) if trim$(s$) = "" then FindEmptyRecord = i           exit for end if       pos = pos+size next i end function code toc

function FieldsFit
This function can be used before saving a record to check if any of the fields would be truncated, in which case users could be warned about data loss. The function returns 0 if no fields would be truncated, or the number of the first field that doesn't fit into the file and needs to be truncated. code format="vb" function FieldsFit for i = 1 to FieldLength(0) if len(RecordField$(i)) > FieldLength(i) then FieldsFit = i           exit for end if   next i end function code Return to the Random access files with arrays page