Login

Set, get, save and load variables

Kohaku's picture
`setup
global datcnt
dim datname(datcnt) as string
dim datentry(datcnt) as string
 
`example
set_dat("hi", "greeting")
set_dat("bye", "not greeting")
 
print get_dat("hi")
print get_dat("bye")
 
set_dat("bye", "really not greeting")
 
print get_dat("hi")
print get_dat("bye")
 
set_dat("hi", "nout")
set_dat("bye", "")
 
print get_dat("hi")
print get_dat("bye")
 
wait key
end
 
`functions
function setDat(datname as string, datentry as string)
	for a = 1 to datcnt
		if datname(a) = datname
			datentry(a) = datentry
			if datentry = ""
				datname(a) = ""
			endif
			exitfunction
		endif
	next a
 
	if datentry <> ""
		for a = 1 to datcnt
			if datname(a) = ""
				datname(a) = datname
				datentry(a) = datentry
				exitfunction
			endif
		next a
 
		inc datcnt
		dim datname(datcnt) as string
		dim datentry(datcnt) as string
		datname(datcnt) = datname
		datentry(datcnt) = datentry
	endif
endfunction
 
function getDat(datname as string)
	for a = 1 to datcnt
		if datname(a) = datname
			null_$ = datentry(a)
			exitfunction null_$
		endif
	next a
	null_$ = ""
endfunction null_$
 
function emptyDat()
	datcnt = 0
	dim datname(datcnt) as string
	dim datentry(datcnt) as string
endfunction
 
function saveDat(filename as string)
	if file exist(filename)
		delete file filename
	endif
	open to write 1, filename
	for a = 1 to datcnt
		if datname(a) <> ""
			write string 1, datname(a)
			write string 1, datentry(a)
		endif
	next a
	close file 1
	null_$ = ""
endfunction null_$
 
function loadDat(filename as string)
	open to read 1, filename
	repeat
		read string 1, rs$
		if rs$ <> ""
			nameEntry$ = rs$
			read string 1, rs$
			dataEntry$ = rs$
			setDat(nameEntry$, dataEntry$)
		endif
	until file end(1)
	close file 1
	null_$ = ""
endfunction null_$
Average: 5 (1 vote)