Quantcast Tables - count items in them - WoWInterface
Thread Tools Display Modes
08-01-16, 09:23 AM   #1
Hiketeia
An Aku'mai Servant
 
Hiketeia's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2016
Posts: 33
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.
  Reply With Quote
08-01-16, 09:35 AM   #2
Dridzt
A Pyroguard Emberseer
 
Dridzt's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2005
Posts: 1,257
# 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

Last edited by Dridzt : 08-01-16 at 09:45 AM.
  Reply With Quote
08-01-16, 11:06 PM   #3
myrroddin
A Pyroguard Emberseer
 
myrroddin's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 1,101
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.
  Reply With Quote
08-02-16, 02:29 AM   #4
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Originally Posted by myrroddin View Post
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.
__________________
Author/maintainer of Grid, PhanxChat, oUF_Phanx, and many more.
Troubleshoot an addonTurn any code into an addonMore addon resources
Need help with your code? Post all of your actual code! Attach or paste your files.
Please don’t PM me about addon bugs or code questions. Post a comment or forum thread instead!
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Tables - count items in them

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off