Re: Parameters by Reference
- Posted by Ralf Nieuwenhuijsen <nieuwen at XS4ALL.NL> Apr 30, 1999
- 486 views
The best way, IMHO, when you want several parts of your program to share data, independent of the 'namespace', you could use special functions: These examples are key-association libraries, but you could write any sort of 'data'-management-library. The first version uses 'host'-storage, the second uses 'client'-store, I will discuss the advantages and disadvantages, afterwards, of the two approaches. You use the key-association libs, as following: -- To store a new value store_key ("mykey", "myval") -- To set an already existing key to another value store_key ("mykey", "myotherval") -- To retrieve a value puts (1, retrieve_key ("mykey") ) -- To 'delete' a key store_key ("mykey", -1) -- Notice the special value -1. -- This value will be returned by retrieve_key when no key matches, and setting a key to this value will 'delete' the key. -- This is not a problem since the value returned by retrieve_key _will_ return the correct value. -- keys_ex_a.e global constant TRUE = 1, FALSE = 0, NONE = -1 sequence keys, data global procedure store_key (object key, object val) integer place place = find (key, keys) if place then if equal (val, -1) then data = data[1..place-1] & data[place+1..length(data)] keys = keys[1..place-1] & keys[place+1..length(keys)] else data[place] = val end if elsif equal (val, -1) data = append(data, val) keys = append(keys, key) end if end procedure global function retrieve_key (object key) integer place place = find (key, keys) if place then return data[place] else return -1 end if end function --- This second example works like this: -- Creating a new data-base keydb my_kl my_kl = new_keydb -- Storing a new key my_kl = store_key (my_kl, "mykey", "myval") -- Setting that key to a new value my_kl = store_key (my_kl, "mykey", "myotherval") -- 'Deleting' that key my_kl = store_key (my_kl, "mykey", NONE) -- none equals -1 -- Retrieving a key puts (1, retrieve_key (my_kl, "mykey") ) ----- keys_ex_b.e -- Client-side data storage global constant TRUE = 1, FALSE = 0, NONE = -1 type keydb (object x) if sequence (x) and length(x) = 2 and sequence (x[1]) and sequence (x[2]) then return length(x[1]) = length(x[2]) else return 0 end if end type global function new_keydb () return {{},{}} end function global function store_key (keydb kl, object key, object val) sequence keys, data integer place keys = kl[1] data = kl[2] place = find (key, keys) if place then if equal (val, -1) then data = data[1..place-1] & data[place+1..length(data)] keys = keys[1..place-1] & keys[place+1..length(keys)] else data[place] = val end if elsif equal (val, -1) data = append(data, val) keys = append(keys, key) end if return {keys, data} end function global function retrieve_key (keydb kl, object key) sequence data, keys integer place keys = kl[1] data = kl[2] place = find (key, keys) if place then return data[place] else return -1 end if end function -------------------------------------------------------------------------------------------------------------------------------- ------------ Advantages of the server-side storage (ps. these terms are made up by me, there are not official technical terms, or anything): + There are no namespace problems. + It easier to use Advantages of the client-side storage: + You can have multiple databases + You can store a database, print it out, or store it inside any type of structure, and therefor you could say, you get a lot more control over the data. You can copy two databases, etc. + Different libraries support the same datatype can work with the data in the same way the library does. (that is, change the variable and return it), you wouldn't notice in difference in handling. And off course all of 'advantages' could be achieved by expending the first example, with many more routines to clear the database, create and setup multiple databases, to store a database, etc. However the initiative can only come from the original library, any user can not later on, add routines to his program that do this, since he can not access the variable at all. Ralf N. nieuwen at xs4all.nl ralf_n at email.com