When you say ‘objects’ do you mean display objects or just table entries?
I think you mean the latter:
-- -- This answer does not address (data persistence) storing and loading data to/from disk. -- local students = {} local function addStudent( uniqueID, firstName, lastName, age, grade) if(students[uniqueID]) then print("Student with ID already exists: ", uniqueID ) end students[uniqueID] = { first = firstName, last = lastName, age = age, grade = grad, uid = uniqueID } end local function getStudentByID( id ) return students[id] end local function getStudentsByGrade( grade ) local tmp = {} for k,v in pairs( students ) do if( grade == v.grade ) then tmp[#tmp+1] = v end end return tmp end local function getStudentsByName( last, first ) local tmp = {} for k,v in pairs( students ) do if( (last == nil or string.lower(last) == string.lower(v.last)] and (first == nil or string.lower(first) == string.lower(v.first) ) then tmp[#tmp+1] = v end end return tmp end
Now you could do this:
addStudent(123456789, "Bill", "Smith", 10 ) addStudent(223456789, "Bill", "Smythe", 9 ) addStudent(323456789, "Sue", "Smith", 10 ) local tmp = getStudentsByGrade( 10 ) for i = 1, #tmp do print( i, tmp[i].first, tmp[i].last, tmp[i].grade ) end -- Gets Bill Smith and Sue Smith Records local tmp = getStudentsByName( nil, "Bill" ) for i = 1, #tmp do print( i, tmp[i].first, tmp[i].last, tmp[i].grade ) end -- Gets Bill Smith and Bill Smythe local tmp = getStudentsByName( ) for i = 1, #tmp do print( i, tmp[i].first, tmp[i].last, tmp[i].grade ) end -- Gets all records.
local tmp = getStudentsByName( "Smith" ) for i = 1, #tmp do print( i, tmp[i].first, tmp[i].last, tmp[i].grade ) end -- Gets Bill Smith and Sue Smith Records