62 posts
Location
Greece
Posted 07 July 2014 - 10:35 AM
Hello guys and I need to ask a question. I have been working on creating a modem(not the ones that normally exist in computercraft) and I needed a table for the people that have connected. Basically it would host their ids. The only problem is that I want it to never end, I mean that I want it to be able to host an infinite number of computers. So is this possible?
7083 posts
Location
Tasmania (AU)
Posted 07 July 2014 - 11:16 AM
The short answer is "no". Eventually your computer will run out of RAM, so an infinite number of anything is out of the question.
The somewhat longer answer is "for all intents and purposes, yes". Tables aren't of a fixed size (unlike the arrays you might be familiar with), so you don't need to know how much you intend to put in them "ahead of time"; you can put however much data in at one time, then add however much data later. In addition, I reckon you'd hit the computer count cap long before you started to push the limits of what tables can hold.
62 posts
Location
Greece
Posted 07 July 2014 - 11:25 AM
The short answer is "no". Eventually your computer will run out of RAM, so an infinite number of anything is out of the question.
The somewhat longer answer is "for all intents and purposes, yes". Tables aren't of a fixed size (unlike the arrays you might be familiar with), so you don't need to know how much you intend to put in them "ahead of time"; you can put however much data in at one time, then add however much data later. In addition, I reckon you'd hit the computer count cap long before you started to push the limits of what tables can hold.
Hmm.. didn't think of that. But what I mean is that I want an amount to be able to "connect" and their ids go in the array. I just don't want to have a specific amount already, I want a lot of people "ahead of time". Thats what I want to know how to do.
Edited on 07 July 2014 - 09:25 AM
7083 posts
Location
Tasmania (AU)
Posted 07 July 2014 - 11:54 AM
Well, that's pretty much exactly what table.insert() lets you do. But I prefer to add new entries like this:
myTable[#myTable + 1] = newEntry
#myTable returns the number of entries in myTable. So myTable[#myTable+1] refers to the first table entry that isn't already filled.
62 posts
Location
Greece
Posted 07 July 2014 - 12:58 PM
Thnx that was really helpful ^_^/> !
62 posts
Location
Greece
Posted 07 July 2014 - 01:16 PM
Bwt something else I want the table to not add the same Id from the computer a second time and because I am not used to using tables I… don't know how it works
570 posts
Posted 07 July 2014 - 01:33 PM
Bwt something else I want the table to not add the same Id from the computer a second time and because I am not used to using tables I… don't know how it works
Check whether the ID already exists in the table before adding it.
You can do that by iterating over the table and if you can find the id, it exists.
local function tblContains(tbl, val)
for _,v in ipairs(tbl) do
if v == val then
--# If the current value in the loop is the ID you're looking for, it exists.
return true
end
end
return false --# If the end of the table was reached without finding the ID, it doesn't exist.
end
local aTable = { 0, 5, 4, 3 }
print(tblContains(aTable, 5)) --# true
print(tblContains(aTable, 2048)) --# false
--# So...
--# Assuming 'id' is the ID to be added:
if not tblContains(aTable, id) then
aTable[#aTable + 1] = id
end
Edited on 07 July 2014 - 11:34 AM