DECLARE SUB sethash (tag$, value$) DECLARE FUNCTION hash$ (s$) DECLARE FUNCTION mkhash! (s$) ' Created:06-24-00 ' By Jeff Connelly ' Associative arrays (sometimes known as hashes) used in QBasic ' This is called associative arrays, and was inspired by GTSC4's question ' ' To retreive a value, use hash$("key") ' To set a value, use sethash("key", "value") ' [QBasic is not extendable enough to allow hash$("key") = "value"] ' This does not need to be accessed directly DIM SHARED hasharray$(60) ' Example program CLS PRINT "Associative Array Example" PRINT DO PRINT DO INPUT "Key to set: ", key$ LOOP WHILE mkhash(key$) > 60 INPUT "Value to set key to: ", value$ sethash key$, value$ PRINT PRINT "Hash values: " FOR i = 1 TO UBOUND(hasharray$) IF (LEN(hasharray$(i)) > 0) THEN PRINT , i, hasharray$(i) NEXT PRINT INPUT "Key to retreive: ", key$ PRINT , key$ + " is " + hash$(key$) LOOP FOR i = 1 TO 14 key$ = STRING$(i, 255) ' Key must be around less than 15 characters at most sethash key$, "bar" PRINT hash$(key$) NEXT FUNCTION hash$ (s$) hash$ = hasharray$(mkhash(s$)) END FUNCTION FUNCTION mkhash (s$) ' Return the checksum of the data in S$. Could use work. Needs to change ' as much as possible even with one bit of change in the data. CRC32 has ' a good polynomial, and CRC16 too, but they are too large for this purpose. DIM ret AS INTEGER FOR i = 1 TO LEN(s$) c$ = MID$(s$, i, 1) c = ASC(c$) IF (i MOD 1) THEN rr = ret ELSE rr = -ret ret = rr + (c * (i / 2)) + i NEXT ' Make the hash within 0..60 for the array ret = INT(ret / 16) IF (ret > 60 OR ret < 0) THEN PRINT "mkhash: error: key too large: " + s$ ret = 0 END IF mkhash = ret END FUNCTION SUB sethash (key$, value$) ' The longest key can be is 14 characters at minimum, depends on bytes hasharray$(mkhash(key$)) = value$ END SUB