I have a table that stores players in a table with their user id as the location. This creates large gaps between each player. Is there a way I can call the first object rather than the first table slot?
you can do something like
local Table = {123456, 982538, 927393}
print(Table[Id])
--Id is whatever player you are trying to find the table is just a example
if this is what you wanted
Let’s just say the table looks like this:
{ nil, nil, player1, nil, player2 }
I want to get the first player but if I say table[1] it would be nil.
Try looping through with an in pairs for loop until the entry isn’t nil, like so:
for i,t in pairs(tableyouwantiguess) do
it t then
--[[
do stuff i guess lol, if you cancel it here with break or
something then it should be the very first non nil variable
--]]
end
end
yes thats what the Id is for, it loops through the table to find any value that is exactly the same as itself if that makes sense.
One of the problems here is iterating in order. Can’t use ipairs because it’ll terminate due to gaps in your table (or nil values). Numeric for loop could work here, where you keep iterating until the first non-nil value and then break. Pairs definitely won’t work because order isn’t guaranteed.
Is there any situation where you’d occur with a gap like this that you need to find the first non-nil value and it’s not implied that all key-value pairs are non-nil? This seems like the product of unpredictability in code. You want to avoid being unpredictable as much as you can, such that an array-like table would have no gaps (meaning the nils aren’t present and you only have P1 and P2).
Why are you putting nil in the table?
It puts players in a table with their place value being their userid.
Use the [1] to get the first element.
I want to get the first element that’s not nil
Then count in the table, [1], [2], get to the one that isn’t nil.
So is there a way I can turn a table into an array with no gaps?
If you want the first value in a table here is an example, Lua indexes start at 1 so, if you did this it would print nil.
local table = {“Blah”, 1, 2}
– if I did this I would get nil because nothing is at the 0th spot. Like I said indexes start at 1.
print(table[0]) - prints nil in output bar.
This will print Blah on the output bar.
print(table[1])
[1] gets the first element of the table and so on, same applies, start at 1 for first element and count up by 1s.
You’d probably have the best luck here by creating a new table and filling it only with elements that are non-nil, then using that as the new table. I don’t know if there’s a better method out there other than addressing the root problem here.
Again: we don’t have a use case nor a scenario, so knowing why you end up with gaps in the first place is odd. It seems like you’re trying to combat unpredictable behaviour, but rather than trying to make that behaviour predictable you’re trying to layer over the problem. This doesn’t actually solve it though.
The code below will work under the assumption that your tables are arrays with gaps, can’t say the same for a dictionary because of lack of order.
local myCollection = {nil, nil, nil, "player1", nil, "player2", nil}
-- Should contain nils and non-nils
print(table.unpack(myCollection, 1, #myCollection))
local function removeGapsInArray(arrayWithGaps)
local gaplessArray = {}
for i = 1, #arrayWithGaps do
local value = arrayWithGaps[i]
if value then
table.insert(gaplessArray, arrayWithGaps[i])
end
end
return gaplessArray
end
myCollection = removeGapsInArray(myCollection)
-- Should not contain nils
print(table.unpack(myCollection, 1, #myCollection))
ipairs doesn’t work here because of the nil keys, so I swapped out for a numeric for loop which will run its course so long as you’re working with numerically ordered indices. It traverses the table in order and then appends non-nil elements to the new table, then returns said new table without the nil.
The length operator is not reliable with arrays that have gaps. A better, but much slower, approach might be to do a pairs loop to determine the number of key-value pairs with numeric indices and to increment a counter in a different until you find that many. Here’s an example of how the length operator can be finnicky:
local x = {2}
x[3] = 2
local y = {2, nil, 2}
local z = {2, nil, 2, nil}
print(#x, #y, #z) --> 1, 3, 1
-- these tables are identical
TIL, cheers. How then would you determine order? I tried to avoid using any generic for loop generators: pairs because order is not guaranteed (which I assume is what OP wants to maintain) and ipairs because the nil values would stop it from continuing execution.