manawydan Posted December 12, 2014 Posted December 12, 2014 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? "Querer não é poder, mas tentar é avançar"!
Dealman Posted December 12, 2014 Posted December 12, 2014 Check if the value is nil. If it is, use table.remove. table.remove(theTable, theIndex); If I help you in a thread and you need further assistance, please don't PM me - use the thread you created instead. This way everyone on the forum can take advantage of it.
DiSaMe Posted December 12, 2014 Posted December 12, 2014 'ipairs' iterates up to first nil value. Which means no subsequent values are processed in your code. -
manawydan Posted December 12, 2014 Author Posted December 12, 2014 thanks, what I could do? use pairs? "Querer não é poder, mas tentar é avançar"!
MTA Team 0xCiBeR Posted December 13, 2014 MTA Team Posted December 13, 2014 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. DevOps Engineer, Cloud Advocate & Security Engineer(Red Team) | Coffee, Containers & Burp
DiSaMe Posted December 13, 2014 Posted December 13, 2014 Also, while ipairs starts at key 1 and increases it, pairs loops through the table in unspecified order. -
manawydan Posted December 13, 2014 Author Posted December 13, 2014 thanks guys! "Querer não é poder, mas tentar é avançar"!
manawydan Posted December 13, 2014 Author Posted December 13, 2014 i try make one new code and work function orderTable(t) local nt = {} for i=1,#t do if(t[i] ~= nil)then table.insert(nt,t[i]) end end return nt end "Querer não é poder, mas tentar é avançar"!
Buffalo Posted December 13, 2014 Posted December 13, 2014 # won't return correct items count, because they are not in order just use your script with pairs function orderTable(t) local nt = {} for k,v in pairs(t) do table.insert(nt,v) end return nt end Powered by Kimsufi© ☢ ZHP on Facebook ☢ ZHP on Youtube ☢ Support us ☢
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now