Jump to content

Table order


manawydan

Recommended Posts

so iam try make this:

local t = {5,nil,2,nil,3} change to this t = {5,2,3}

i try make this but no work

function orderTable(t) 
local nt = {} 
local c = 0 
for k,v in ipairs(t) do 
if(v ~= nil)then 
c = c +1 
print(c) 
table.insert(nt,v) 
end 
end 
return nt 
end 
  
  
addCommandHandler("ta", 
function() 
local t = {5,nil,2,nil,3} 
j = orderTable(t) 
for k,v in ipairs(j)do 
print(tostring(v)) 
end 
end) 

any help?

Link to comment

Yes, the solution is to use pairs as ipairs uses an iterator that only returns pairs of values attributed to numerical keys. This means that they can only return values defined in list format. In addition, if ipairs encounters any nil value, it will stop in its tracks, pairs, on the other hand, iterates through the entire table and returns keys that are non-numerical as well, so that's why you can't use ipairs to do the job.

Hope it helps.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...