WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   Tables - count items in them (https://www.wowinterface.com/forums/showthread.php?t=54087)

Hiketeia 08-01-16 09:23 AM

Tables - count items in them
 
I'm missing something here... transitioning from other languages to lua and I've got a gap somewhere I was hoping you could point it out to me and give me some reference material to fill in the gap...

I don't seem to be understanding tables correctly -- mostly the '#' meta and iterating over them. Here is some code:

Lua Code:
  1. function testTableAdd(count)
  2.    local t = {}
  3.    for i = 1, count do
  4.       print("Adding "..i)
  5.       t[i] = "Yes please ".. i
  6.       print(#t)
  7.    end
  8.    print("We counted "..#t.." times")
  9.    return t
  10. end
  11. print(testTableAdd(5))
  12. function testTableAddOutOfOrder()
  13.    local t = {}
  14.    t[3] = "three"
  15.    t[5] = "five"
  16.    t[15] = "wow"
  17.    print("We counted "..#t.." times")
  18.    return t
  19. end
  20. print(testTableAddOutOfOrder())

I was expecting "We counted 5 times" and "We counted 3 times", but the 2nd one says 0 -- but it has the 3 entries.

For the first I can iterate through them with a for i = 1, #t do but the second one that doesn't work since the # is 0. Using a 'pairs' do I got through the second. Hmmm... typing it out it is making more sense why the second loop was failing, because the indexes were different. But some background would still help.

I wonder in my bigger case if I should just fill in the gaps with nils and that might just solve my problem...

Is there a way to add to the table, just to the end of it?

I'm used to

t << 'new entry'

in something like ruby.

Thanks.

Dridzt 08-01-16 09:35 AM

# only works for array-type tables with no "holes" in them.

For anything else you need an iterator and a counter variable using pairs()

Edit: Reference: http://www.lua.org/manual/5.2/manual.html#3.4.6

Edit2: For your other questions: http://www.lua.org/manual/5.2/manual.html#6.5

myrroddin 08-01-16 11:06 PM

You should also define the table t outside the function. Right now you are (re)creating the table every time the function runs, which is terribad.

Phanx 08-02-16 02:29 AM

Quote:

Originally Posted by myrroddin (Post 317303)
You should also define the table t outside the function. Right now you are (re)creating the table every time the function runs, which is terribad.

For a testing/debugging function, there's nothing wrong with recreating the table every time the function runs.

For production functions, it depends on the purpose of the table, and the frequency with which the function will be run. If it's an OnUpdate script, or running in response to UNIT_AURA events, yes, creating a new table on each run is a big no-no. If it's running in response to a user entering a slash command, or an infrequent event like PLAYER_ENTERING_WORLD, it's not really a big deal, and you should do whatever makes more sense scope-wise for the content/purpose of the table.


All times are GMT -6. The time now is 05:13 AM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI