Thread Tools Display Modes
05-16-20, 08:03 AM   #1
jettisonedintospace
A Defias Bandit
Join Date: Aug 2019
Posts: 2
Question Table > how to pick or skip values?

hi,

I've searched for a few hours in vain here and on the web but I fail to pick the proper keywords to find the answer for my question.

Lua Code:
  1. table = {
  2.         [1] = "One",
  3.         [2] = "Two",
  4.         [3] = "Three",
  5.         [4] = "Four",
  6.         [5] = "Five",
  7.     }
  8.     for i=1, 3 do
  9.         table[i] = DEFAULT_CHAT_FRAME:AddMessage("test"..tostring(i))
  10.     end

will print:
test1
test2
test3

but what if I want to print:
test1
test2
test4
test5

how to skip a value?

or pick only a few that are not consecutive?

like for:
test2
test5
  Reply With Quote
05-16-20, 09:16 AM   #2
Xrystal
nUI Maintainer
 
Xrystal's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Feb 2006
Posts: 5,877
As with any array you either get them in order ..
Lua Code:
  1. for i = 1,5 do
  2.     print(items[i])
  3. end

or select them by index
Lua Code:
  1. print(items[10])

or select them by key if they are not in sequential index order
Lua Code:
  1. print(items["priest"])


If you want to go through a list and print certain ones you will need an additional check, using some set of if statements that will tell you if that item is the one that fits the bill. In this example the items table contains a class name to signify that item is for priests only. So if you want to print off all those items suitable for priests then you would do that test. How you would use this all depends on what information your data includes.
Lua Code:
  1. for i = 1,5 do
  2.     if items[i].class == "priest" then
  3.        print(items[i])
  4.     end
  5. end
This is a link to how tables work in lua which is the equivalent to arrays
https://www.lua.org/pil/2.5.html
__________________
  Reply With Quote
05-16-20, 09:49 AM   #3
jettisonedintospace
A Defias Bandit
Join Date: Aug 2019
Posts: 2
thank you, I'll look into it.
  Reply With Quote
05-16-20, 03:35 PM   #4
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
For further practice on working with tables, see here:
http://lua-users.org/wiki/TablesTutorial (basic)
http://lua-users.org/wiki/TableLibraryTutorial (a little more advanced)
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Table > how to pick or skip values?

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