This commit is contained in:
Ingy döt Net 2013-04-10 16:19:29 -07:00
parent e5e8880e41
commit 518da4a923
1019 changed files with 15877 additions and 0 deletions

View file

@ -0,0 +1,106 @@
global values$, keys$
dim values$[1]
dim keys$[1]
call updateKey("a","apple")
call updateKey("b","banana")
call updateKey("c","cucumber")
gosub show
print "I like to eat a " + getValue$("c") + " on my salad."
call deleteKey("b")
call updateKey("c","carrot")
call updateKey("e","endive")
gosub show
end
show:
for t = 0 to countKeys()-1
print getKeyByIndex$(t) + " " + getValueByIndex$(t)
next t
print
return
subroutine updateKey(key$, value$)
# update or add an item
i=findKey(key$)
if i=-1 then
i = freeKey()
keys$[i] = key$
end if
values$[i] = value$
end subroutine
subroutine deleteKey(key$)
# delete by clearing the key
i=findKey(key$)
if i<>-1 then
keys$[i] = ""
end if
end subroutine
function freeKey()
# find index of a free element in the array
for n = 0 to keys$[?]-1
if keys$[n]="" then return n
next n
redim keys$[n+1]
redim values$[n+1]
return n
end function
function findKey(key$)
# return index or -1 if not found
for n = 0 to keys$[?]-1
if key$=keys$[n] then return n
next n
return -1
end function
function getValue$(key$)
# return a value by the key or "" if not existing
i=findKey(key$)
if i=-1 then
return ""
end if
return values$[i]
end function
function countKeys()
# return number of items
# remember to skip the empty keys (deleted ones)
k = 0
for n = 0 to keys$[?] -1
if keys$[n]<>"" then k++
next n
return k
end function
function getValueByIndex$(i)
# get a value by the index
# remember to skip the empty keys (deleted ones)
k = 0
for n = 0 to keys$[?] -1
if keys$[n]<>"" then
if k=i then return values$[k]
k++
endif
next n
return ""
end function
function getKeyByIndex$(i)
# get a key by the index
# remember to skip the empty keys (deleted ones)
k = 0
for n = 0 to keys$[?] -1
if keys$[n]<>"" then
if k=i then return keys$[k]
k++
endif
next n
return ""
end function

View file

@ -0,0 +1,21 @@
REM Store some values with their keys:
PROCputdict(mydict$, "FF0000", "red")
PROCputdict(mydict$, "00FF00", "green")
PROCputdict(mydict$, "0000FF", "blue")
REM Retrieve some values using their keys:
PRINT FNgetdict(mydict$, "green")
PRINT FNgetdict(mydict$, "red")
END
DEF PROCputdict(RETURN dict$, value$, key$)
IF dict$ = "" dict$ = CHR$(0)
dict$ += key$ + CHR$(1) + value$ + CHR$(0)
ENDPROC
DEF FNgetdict(dict$, key$)
LOCAL I%, J%
I% = INSTR(dict$, CHR$(0) + key$ + CHR$(1))
IF I% = 0 THEN = "" ELSE I% += LEN(key$) + 2
J% = INSTR(dict$, CHR$(0), I%)
= MID$(dict$, I%, J% - I%)

View file

@ -0,0 +1,24 @@
::assocarrays.cmd
@echo off
setlocal ENABLEDELAYEDEXPANSION
set array.dog=1
set array.cat=2
set array.wolf=3
set array.cow=4
for %%i in (dog cat wolf cow) do call :showit array.%%i !array.%%i!
set c=-27
call :mkarray sicko flu 5 measles 6 mumps 7 bromodrosis 8
for %%i in (flu measles mumps bromodrosis) do call :showit "sicko^&%%i" !sicko^&%%i!
endlocal
goto :eof
:mkarray
set %1^&%2=%3
shift /2
shift /2
if "%2" neq "" goto :mkarray
goto :eof
:showit
echo %1 = %2
goto :eof

View file

@ -0,0 +1,8 @@
h = [:] #Empty hash
h[:a] = 1 #Assign value
h[:b] = [1 2 3] #Assign another value
h2 = [a: 1, b: [1 2 3], 10 : "ten"] #Initialized hash
h2[:b][2] #Returns 3