Jump to content

Looping through table/array


Recommended Posts

Hi

Sometimes i hear word "table", sometimes "array", are they the same for LUA? Or this is another types?

I was always not sure about it, and looping through it.

Every loop on ipairs (this is looping through tables/array, right?) i've written i've copied from wiki, then modified.

Now look what im trying to do:

texts= { }
textCount=0
function showText(text, time)
textCount = textCount+1
texts[textCount]={ }
texts[textCount]['text']=text
texts[textCount]['time']=time
end
 
-- for example I'm adding some texts to that table/array
showText("test1",50)
showText("test2",33)
showText("test3",99)
 
-- then after some conditions im removing one of them
texts[1]=nil
 
-- so only texts[0] and texts[2] are not nil
 
-- how can i loop through all texts, w/out using iteration, and checking if texts[i] is nil, or not nil ?
-- for example output all texts, and times assigned to them to chatbox?

Link to comment

"Table" is the datastructure of Lua that can be used as an array, list or even class.

The ipairs() iterator only works with increasing indices (1,2,3,4, ..). If you want to loop through all numbers, use a numeric for loop:

for i = 1,10 do .. end -- loops from 1 to 10 in steps of 1

If you want to loop through all elements of a table in no particular order, use pairs():

for k,v in pairs(table) do .. end

Maybe you also want to use table.insert() and table.remove() which shifts the indices to create increasing indices.

http://lua-users.org/wiki/ForTutorial

http://www.lua.org/manual/5.1/manual.html#5.5

  • Like 1
Link to comment

btw you could use #[tablename] for getting tables sizes, not with non-needed textCount = textCount+1

in current example you could use:

texts[#texts+1]={ }
texts[#texts+1]['text']=text
texts[#texts+1]['time']=time
--or
table.insert(texts,{})
table.insert(texts["text"],text)
table.insert(texts["time"],time)

Link to comment

nice idea karlis :)

but im not sure which one is better (for speed of the script),

- storing one integer variable (takes a bit of memory),

- or checking table size every add (takes a bit of CPU)

table.insert isn't good idea for me, as after that i will need to get insert key, so again, checking the table size, etc, which will result in longer script :P

Link to comment
anyway we are talking about few miliseconds, so that, how script is readable and easy to write/edit is much more important

i know, but almost no difference in looking,

and ive got a lot of scripts, and they are slowing game a bit, i'll optimize the rest (some are downloaded), and new ones i wanna make perfect.. im a programmer, so im caring about details ;)

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...