How to Create an Array of Objects

Hello,

I would like to be able to create an array of objects that have defined attributes. Essentially, in my app I will be iterating through a created array of “students” and each student will have the attributes: “name and test_score.”

I’m imagining something like this:

students = {}

student_info{

name =

test_score =

}

Excuse my syntax because I know I am missing key information.

As I iterate through the array of students, I need to be able to access each student’s student_info.

Thanks for the help!

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

"array of students" just screams “help me with my coursework”

From a first time poster as well.  I think these forums are getting a reputation for doing other people’s homework.

Thanks to roaminggamer for the help. I did mean to say table entries. I’m glad to see that there are people that are willing to lend help on this forum to someone tired of his career and giving app development a try. I am also glad that you, roaminggamer were able to use my example of student to answer the question knowing that I would use a simple example and not give away exactly what I’ll be doing in my app. Thank goodness this forum only has great programmers willing to help and no trolls…

Thanks again :slight_smile:

Live long and prosper…

@pmd0005 - You’re welcome, but don’t give me too much credit. 

I am a bit harsh when answering folks sometimes.  I shouldn’t be and I’m trying not to do it anymore, but I slip occasionally.

Cheers,

Ed

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

"array of students" just screams “help me with my coursework”

From a first time poster as well.  I think these forums are getting a reputation for doing other people’s homework.

Thanks to roaminggamer for the help. I did mean to say table entries. I’m glad to see that there are people that are willing to lend help on this forum to someone tired of his career and giving app development a try. I am also glad that you, roaminggamer were able to use my example of student to answer the question knowing that I would use a simple example and not give away exactly what I’ll be doing in my app. Thank goodness this forum only has great programmers willing to help and no trolls…

Thanks again :slight_smile:

Live long and prosper…

@pmd0005 - You’re welcome, but don’t give me too much credit. 

I am a bit harsh when answering folks sometimes.  I shouldn’t be and I’m trying not to do it anymore, but I slip occasionally.

Cheers,

Ed