|
Post by bluatigro on Oct 11, 2018 11:06:38 GMT
i want to make the code as fast as posible that is that i do not use a key$() and a dict$()
i have tryed this before but i lost the code to a ransom virus not jet good :
dim dict$( 100 ) global keylenght , key$ , dicttel keylenght = 10 dicttel = 0
k$ = "this is a test" v$ = "10 20 30 40"
for i = 1 to 4 call additem word$( k$ , i ) , word$( v$ , i ) next i print key$ for i = 1 to dicttel print word$( k$ , i ) , getitem$( word$( k$ , i ) ) next i call remove "is" print key$ for i = 1 to dicttel print word$( k$ , i ) , getitem$( word$( k$ , i ) ) next i
end sub remove k$ p = instr( key$ , index$( k$ ) ) for i = p / keylenght to dicttel dict$( i ) = dict$( i + 1 ) next i key$ = left$( key$ , p ) + right$( key$ , p + keylenght ) dicttel = dicttel - 1 end sub function getitem$( q$ ) p = instr( key$ , index$( q$ ) ) - 1 getintem$ = dict$( p / 10 ) end function sub additem k$ , q$ if dicttel > 100 then exit sub dict$( dicttel ) = q$ dicttel = dicttel + 1 key$ = key$ + index$( k$ ) end sub function index$( q$ ) index$ = left$( q$ + space$( keylenght ) , keylenght ) end function
|
|
|
Post by B+ on Oct 12, 2018 14:01:28 GMT
bluatigro, What a mess! We had it worked out pretty well 2 years ago! Here is old code you might be looking for, it appears to be functioning properly.
To all,
Recall a dictionary is a string of definitions: key or word + a separator + value or the words definition + another kind of separator... repeat The word and definition are also called Key Value pairs, the Keys of a Dictionary must be unique whereas you can use the same Value for several Keys.
cutKey$ and cutValue$ are bluatigro's terms as I recall, I might call cutKey$ the kvDivider and cutValue$ the kvDefinition delimiter. The main thing to remember is that there are two kinds of separators being used: one that divides the Key and Value and another that divides the KV pairs.
'Dictionary strings tools.txt for JB v1.01 [B+=MGA] 2016-07-26
' modify single string dictionaries to use instr search for key to find value
' definition of a Dictionary string: ' A string that alters between key$ and value$ with global cutKey$ dividing them ' and starting and ending each key|value pair with global cutValue$.
global cutKey$, cutValue$ cutKey$ = "|" cutValue$ = "~" OK = addKV( "", "nothing", bluStr$) 'test tolerance of empty strings if OK = 1 then print "oops!" OK = addKV( "", "", bluStr$) if OK = 1 then print "oops!" OK = addKV( "i", "mi", bluStr$) if OK = 0 then print "oops!" OK = addKV( "you", "vi", bluStr$) if OK = 0 then print "oops!" OK = addKV( "he" , "li", bluStr$) if OK = 0 then print "oops!" OK = addKV( "hi", "greetings", bluStr$) if OK = 0 then print "oops!" OK = addKV( "hi you", "personal greeting", bluStr$) if OK = 0 then print "oops!" OK = addKV( " i ", "space i space", bluStr$) if OK = 0 then print "oops!" OK = addKV("he", "not she", bluStr$) if OK = 1 then print "oops!" print " value for key = empty string",valueOf$( "", bluStr$) print " value for key = i",valueOf$( "i", bluStr$) print " value for key = you",valueOf$( "you", bluStr$) print " value for key = he",valueOf$( "he", bluStr$) print "value for key = q (not stored)",valueOf$( "q", bluStr$) print "value for key = hi (ends in i)",valueOf$( "hi", bluStr$) print " value for key = hi you",valueOf$( "hi you", bluStr$) print " value for _i_ where _ = space",valueOf$( " i ", bluStr$) print print "Entire dictionary string:" print bluStr$ print print "Definitions count = ";defCount(bluStr$) print print "Test finding keys for given values:" print " First key found for space i space =", "'";keyOf$("space i space", bluStr$);"'" print " First key found for mi =", "'";keyOf$("mi", bluStr$);"'" print " First key found for you (a key) =", "'";keyOf$("you", bluStr$);"' no you as value" print "First key found for not me (not value or key) =", "'";keyOf$("not me", bluStr$);"'" print print "Test cutKV (the opposite of addKV) cut by key given:" print OK = cutKV("hi", bluStr$) if OK then print "hi|greetings should be removed:" else print "oops!" print bluStr$ print OK = cutKV("i", bluStr$) if OK then print "i|me should be removed:" else print "oops!" print bluStr$ print OK = cutKV(" i ", bluStr$) if OK then print "' i |space i space' should be removed:" else print "oops!" print bluStr$ print OK = cutKV("nothing", bluStr$) if OK then print "oops!" else print "Nothing was removed because key was 'nothing'." print bluStr$ print print "Test keyList$ function:" print keyList$(bluStr$) print print "Test editKV(i,me, bluStr$)" OK = editKV("i","me", bluStr$) print bluStr$ print print "Test editKV(i,me, bluStr$)" OK = editKV("i","mi", bluStr$) print bluStr$ print print "end of sample program"
function keyList$(source$) keyCount = defCount(source$) 'this counts cutKey$, 'there is one more cutValue$ at beginning if keyCount then b$ = "" for n = 2 to keyCount + 1 'skip first cutValue$ test$ = word$(source$, n, cutValue$) 'chr$(10) ??? commas and spaces too common, 'chr$(10) actually prints out like list b$ = b$ + chr$(10) + word$(test$, 1, cutKey$) next keyList$ = right$(b$, len(b$)-1 ) else keyList$ = "" end if end function
function editKV(key$, editValue$, byref source$) OK = cutKV(key$, source$) OK = addKV(key$, editValue$, source$) editKV = OK end function
function cutKV(key$, byref source$) if key$ = "" or len(source$) = 0 then cutKV = 0 : exit function find$ = cutValue$ + key$ + cutKey$ place = instr(source$, find$) if place then last = instr(source$, cutValue$, place + 1) source$ = mid$(source$, 1, place) + mid$(source$, last + 1) cutKV = 1 else cutKV = 0 end if end function
function addKV(key$, value$, byref source$) if key$ <> "" and value$ <> "" then if valueOf$(key$, source$) = "" then if len(source$) = 0 then source$ = cutValue$ source$ = source$ + key$ + cutKey$ + value$ + cutValue$ addKV = 1 else addKV = 0 end if else addKV = 0 end if end function
function keyOf$(value$, source$) 'this finds key for first value$ match found with instr 'depending on use of dictionary strings may want to find all keys 'with same value if value$ = "" then keyOf$ = "" : exit function find$ = cutKey$ + value$ + cutValue$ place = instr(source$, find$) if place then b$ = "" last = place - 1 while mid$(source$, last, 1) <> cutValue$ b$ = mid$(source$, last, 1) + b$ last = last - 1 wend keyOf$ = b$ else keyOf$ = "" end if end function
function valueOf$(key$, source$) if key$ = "" then valueOf$ = "" : exit function find$ = cutValue$ + key$ + cutKey$ place = instr(source$, find$) if place then start = instr(source$, cutKey$, place) + 1 last = instr(source$, cutValue$, start) valueOf$ = mid$(source$, start, last - start) else valueOf$ = "" end if end function
function defCount(s$) 'note the count of cutValue$ characters would be one more if len(s$) then for i = 1 to len(s$) if mid$(s$, i, 1) = cutKey$ then cnt = cnt + 1 next defCount = cnt else defCount = 0 end if end function
|
|