VLS - examples
All examples are in SBASIC so they will need to be modified for SuperBASIC
if required. SuperBASIC may also require some additional toolkit commands
for some of the examples.
RUNable examples assume the standard 3-window QL console setup.
Save and load a simple array:
n% = 273: m% = 10: rem Dimensions
DIM arr1$(n%, m%)
:
FOR i% = 0 TO n%
arr1$(i%) = FILL$(CHR$(RND(65 TO 90)), RND(1 TO 10))
END FOR i%
:
CLS
PRINT 'Array 1:'! DIMN(arr1$)! DIMN(arr1$(0))! arr1$(0)
ERT VSAVE("ram1_data_dat"\ n%, m%, arr1$)
:
rem Re-load array data into a new array
:
er = VLOAD(ram1_data_dat, n%, m%)
IF er <> -1: ERT er: rem Incomplete is normal
DIM arr2$(n%, m%)
:
ERT VLOAD(ram1_data_dat, n%, m%, arr2$)
PRINT 'Array 2:'! DIMN(arr2$)! DIMN(arr2$(0))! arr2$(0)
Save and load a partial array:
100 n% = 273: m% = 10: x% = 10: rem Dimensions
110 DIM arr1$(n%, m%)
120 :
130 FOR i% = 0 TO n%
140 arr1$(i%) = FILL$(CHR$(RND(65 TO 90)), RND(1 TO 10))
150 END FOR i%
160 :
170 CLS
180 PRINT 'Array 1:'! DIMN(arr1$)! DIMN(arr1$(0))! arr1$(0)
190 ERT VSAVE("ram1_data_dat"\ x%, m%, arr1$(0 TO x%))
200 :
210 rem Re-load array data into a new array
220 :
230 er = VLOAD(ram1_data_dat, n%, m%)
240 IF er <> -1: ERT er: rem Incomplete is expected
250 DIM arr2$(n%, m%)
260 :
270 ERT VLOAD(ram1_data_dat, n%, m%, arr2$)
280 PRINT 'Array 2:'! DIMN(arr2$)! DIMN(arr2$(0))! arr2$(0)
Load array data into larger array:
Replace code from line 230+ with this:
230 er = VLOAD(ram1_data_dat, n%, m%)
240 IF er <> -1: ERT er: rem Incomplete is normal
250 x% = n% + 90: rem Extra room required
260 DIM arr2$(x%, m%)
270 :
280 er = VLOAD(ram1_data_dat, n%, m%, arr2$)
290 IF er <> -10: ERT er: rem EOF is expected
300 PRINT 'Array 2:'! DIMN(arr2$)! DIMN(arr2$(0))! arr2$(0)
Creating a VSAVE file from SBASIC:
The example below comes from an actual application, so may seem quite
complex taken out of context.
Note that to avoid clutter this example doesnt add any actual data to the
variables and arrays.
Some program data is to be saved in the file:
- fnm$ is the VSAVE file name (the name is not saved)
- dftnm$ is a string variable containing some data
- count% is the number of active items in the main data arrays
- DATE is the current time (ie time of saving)
- bal is a subsidiary float array
- dt%, ref, text$, amt, out%, in%, rems$ are the main data arrays
So lets DIMension the main data arrays; m% is some number, say 100:
DIM dt%(m%), ref(m%), text$(m%, 22), out%(m%), in%(m%), amt(m%), rems$(m%, 14)
DIM bal(1, 4), dftnm$(8): rem Some subsidiary arrays
Normally the whole data set will be saved to a file with a simple VSAVE, eg:
er% = VSAVE(fnm$, dftnm$, count%, DATE, bal, dt%(TO count%), ref(TO count%), text$(TO count%), amt( >>
TO count%), out%(TO count%), in%(TO count%), rems$(TO count%))
and could be loaded back in the same state with:
er% = VLOAD(fnm$,, count%)
IF er% <> -1: DoError: rem OOps! Bad error (eg file not found)
DIMension the arrays and then load:
m% = count% + 10: rem Reserve room for saved data plus ten more items
DIM dt%(m%), ref(m%), text$(m%, 22), out%(m%), in%(m%), amt(m%), rems$(m%, 14)
DIM bal(1, 4), dftnm$(8): rem Fixed dimensions known by program
Now load the data in the top part of each array:
er% = VLOAD(fnm$, dftnm$, count%, lastupd, bal, dt%(TO count%), ref(TO count%), text$(TO count%), >>
amt(TO count%), out%(TO count%), in%(TO count%), rems$(TO count%))
The index (pointer) array, idx%, indicates selected items and/or order of
sorted data. For now its just initialised to 0..m%:
DIM idx%(m%): FOR i% = 0 TO m%: idx%(i%) = i%: rem Initialise index
Say some operaton has been performed on the data (sorting and/or selection,
for example) and although all the original data is present, we only want
to save those items pointed to by the index array idx%. count% is the number
of elements used in all the arrays (but not necessarily the total number
avialable). idx% itself could have fewer elements than count%, so I have
used the variable max% to indicate the number of items to save:
DEFine FuNction SAVE_SELECT(file$, max%)
LOCal fil%, i%, j%, c%
:
fil% = FOP_OVER(file$): IF fil% < 0: RETurn fil%
c% = max% + 1: rem Number of elements to save (possibly less than count%)
BPUT#fil%; 'VS01': rem Identifying flag
WPUT#fil%; 11: rem Parameter count
LPUT#fil%; 22: rem Skiplength = offset to next value
WPUT#fil%; $301, 1, 8, 1: rem Single dimension array
PUT#fil%; dftnm$: rem Output array element
BPUT#fil%; FILL$(" ", 8 - LEN(dftnm$)): rem Add padding to 8 char
:
LPUT#fil%; 8: rem Skiplength to next
WPUT#fil%; $203, max%: rem integer count
:
LPUT#fil%; 12: rem Skiplength
WPUT#fil%; $202: rem float
PUT#fil%; DATE: rem Add current date as a variable
:
LPUT#fil%; 76: rem Skiplength
WPUT#fil%; $302, 2, 1, 5, 4, 1: rem bal's array descriptor
FOR i% = 0 TO 1
FOR j% = 0 TO 4: PUT#fil%; bal(i%, j%) :rem Add bal's values
END FOR i%
:
rem Main data arrays:
:
LPUT#fil%; 12 + c% * 2: rem Skiplength
WPUT#fil%; $303, 1, max%, 1: rem dt%'s array descriptor
:
rem Selective data from dt's array added to file:
FOR i% = 0 TO max%: WPUT#fil%; dt%(idx%(i%))
:
rem And similarly for the remaining arrays:
:
LPUT#fil%; 12 + c% * 6: rem Skiplength
WPUT#fil%; $302, 1, max%, 1: rem ref
FOR i% = 0 TO max%: PUT#fil%; ref(idx%(i%))
:
LPUT#fil%; 16 + c% * 24: rem Skiplength
WPUT#fil%; $301, 2, max%: rem text$ ..
WPUT#fil%; 24, 22, 1: rem Descriptor
FOR i% = 0 TO max%
PUT#fil%; text$(idx%(i%)): rem Data + padding
BPUT#fil%; FILL$(" ", 22 - LEN(text$(idx%(i%))))
END FOR i%
:
LPUT#fil%; 12 + c% * 6: rem Skiplength
WPUT#fil%; $302, 1, max%, 1: rem amt
FOR i% = 0 TO max%: PUT#fil%; amt(idx%(i%))
:
LPUT#fil%; 12 + c% * 2: rem Skiplength
WPUT#fil%; $303, 1, max%, 1: rem out
FOR i% = 0 TO max%: WPUT#fil%; out%(idx%(i%))
:
LPUT#fil%; 12 + c% * 2: rem Skiplength
WPUT#fil%; $303, 1, max%, 1: rem in
FOR i% = 0 TO max%: WPUT#fil%; in%(idx%(i%))
:
LPUT#fil%; 16 + c% * 16: rem Skiplength
WPUT#fil%; $301, 2, max%: rem rems$ ..
WPUT#fil%; 16, 14, 1: rem Descriptor
FOR i% = 0 TO max%
PUT#fil%; rems$(idx%(i%))
BPUT#fil%; FILL$(" ", 14 - LEN(rems$(idx%(i%))))
END FOR i%
LPUT#fil%; 0: rem The ende
CLOSE#fil%: rem Done!
RETurn 0
END DEFine SAVE_SELECT
The routine above is, of course, not universal, it is designed for a
specific data set. However, it does show how each kind and type of
variable is added to the file, creating new arrays based, in this case,
on data selected via an index. If the index was used to sort the data on
one or more keys in a logical order, then using VLOAD would load that
data again with that as the physical order.
Conditions of use and DISCLAIMER as per Knoware.no
QL Software
