lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Friday, August 12, 2011 06:52:33 PM Lars Doelle wrote:
> Hi All,
> 
> 
> reviewing the tables, i found them limited w.r.t. composed keys.
> To ensure, all keys stay the same during their existience, tables
> as possible (and only) structuring technic are only identified
> by their pointer (i.e. object name).
> 
> --
> 
> Practical example:
> 
>   Key1 = { first="John", last="Doe" }
>   Key2 = { first="John", last="Doe" }
> 
> When using a key like the above for an table, currently Key1
> and Key2 would identify different entries, i.e. are of no use.

Of course, they WOULD be of use if there were two different John Doe's, 
because each one would need his own record. 

From a data processing and table normalization viewpoint, I think 
you'd want the key to be a *meaningless* unique value, with the value 
to be a table containing all of that entities data, in this case first 
and last name, birthday, etc. So your entire table might look 
something like this academic example:

===================================
#!/usr/bin/lua

--DECLARE THE DATABASE
local index = {}
local db = {}

--ADD RECORDS
db[11211]={first="John", last="Doe", job="Police Officer"} 
db[11212]={first="Steve", last="Litt", job="Author"}
db[11213]={first="Lars", last="Doelle", job="Developer"}     
db[11214]={first="Fred", last="Doe", job="Baseball Player"}

--SHOW TWO RECORDS
print(db[11213].last)
print(db[11212].last)

--MAKE REVERSE LOOKUP ON LAST NAME
for k, v in pairs(db) do
	if index[v.last] == nil then
		index[v.last] = {k}
	else
		table.insert(index[v.last], k)
	end
end


--LOOK UP ALL DOE'S
local reclist = index["Doe"]
for k, v in pairs(reclist) do
	print(string.format("Recno %s, %s %s, is a %s",
		tostring(v),
		tostring(db[v].first),
		tostring(db[v].last),
		tostring(db[v].job)
		))
end
===================================

SteveT

Steve Litt
Author: The Key to Everyday Excellence
http://www.troubleshooters.com/bookstore/key_excellence.htm
Twitter: http://www.twitter.com/stevelitt