Thread Tools Display Modes
06-05-22, 07:07 AM   #1
nonexistentx_x
A Murloc Raider
 
nonexistentx_x's Avatar
Join Date: Mar 2019
Posts: 8
simple table loop

Hello. I'm trying to do simple loop table, but it doesn't quite work...
What i want? - Output value each time from my table through onclick function. For example:
We have some table like:
Lua Code:
  1. value = "TWO", -- default
  2. options = { "ONE", "TWO", "THREE", "FOUR" },

and ipairs this table:
Lua Code:
  1. ['onclick'] = function(self)
  2.         for k, v in ipairs(self.options) do
  3.             self.valueText:SetText(v)
  4.             self:set(v)
  5.         end
  6.         -- self.callback(self.check)
  7.     end,
but when i click it changes the value only once, to the latest one and stop.
How to make it change every time on click? (e.g if "TWO" --> set "THREE", or if "FOUR" then set "ONE" and loop again)
  Reply With Quote
06-05-22, 08:34 AM   #2
Kanegasi
A Molten Giant
 
Kanegasi's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2007
Posts: 666
Every time you click, that function is looping through the whole options table, from ONE to FOUR, calling SetText() and set() for each one. No matter how many times you click, you will always see FOUR, while the other three attempted to show within the frame you clicked.

I'm going to assume your default value is also in this frame since options is there.

Lua Code:
  1. function(self)
  2.     local valueset
  3.     if not self.firstload then
  4.         self.firstload=true
  5.         valueset=self.value
  6.         for k,v in ipairs(self.options) do
  7.             if v=self.value then
  8.                 self.lastindex=k
  9.             end
  10.         end
  11.     else
  12.         valueset=self.options[#self.options==self.lastindex and 1 or self.lastindex+1]
  13.     end
  14.     self.valueText:SetText(valueset)
  15.     self:set(valueset)
  16. end
  Reply With Quote
06-05-22, 10:12 AM   #3
nonexistentx_x
A Murloc Raider
 
nonexistentx_x's Avatar
Join Date: Mar 2019
Posts: 8
Originally Posted by Kanegasi View Post
Every time you click, that function is looping through the whole options table, from ONE to FOUR, calling SetText() and set() for each one. No matter how many times you click, you will always see FOUR, while the other three attempted to show within the frame you clicked.

I'm going to assume your default value is also in this frame since options is there.

Lua Code:
  1. function(self)
  2.     local valueset
  3.     if not self.firstload then
  4.         self.firstload=true
  5.         valueset=self.value
  6.         for k,v in ipairs(self.options) do
  7.             if v=self.value then
  8.                 self.lastindex=k
  9.             end
  10.         end
  11.     else
  12.         valueset=self.options[#self.options==self.lastindex and 1 or self.lastindex+1]
  13.     end
  14.     self.valueText:SetText(valueset)
  15.     self:set(valueset)
  16. end
Thx for answer! But it only fires twice and stops, if i set self.firstload=false i get loop only between "ONE" and "TWO" values:
Lua Code:
  1. ['onclick'] = function(self)
  2.         local valueset
  3.         -- if (not value) then value = self:get() end
  4.         -- self.save[self.key] = valueset
  5.         -- self.value =
  6.         if not self.firstload then
  7.             self.firstload = true
  8.             valueset = self.value
  9.             for k, v in ipairs(self.options) do
  10.                 if v == self.value then
  11.                     self.lastindex = k
  12.                     print('k')
  13.                 end
  14.             end
  15.         else
  16.             self.firstload = false -- about this
  17.             valueset = self.options[#self.options == self.lastindex and 1 or self.lastindex+1]
  18.             print('+1')
  19.         end
  20.         self.valueText:SetText(valueset)
  21.         self:set(valueset)
  22.     end,

https://gfycat.com/vibrantorganicbufeo
  Reply With Quote
06-05-22, 10:29 AM   #4
Kanegasi
A Molten Giant
 
Kanegasi's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2007
Posts: 666
Oops, forgot to actually save the last index lol.

Lua Code:
  1. function(self)
  2.     local valueset
  3.     if not self.firstload then
  4.         self.firstload=true
  5.         valueset=self.value
  6.         for k,v in ipairs(self.options) do
  7.             if v=self.value then
  8.                 self.lastindex=k
  9.             end
  10.         end
  11.     else
  12.         local index=#self.options==self.lastindex and 1 or self.lastindex+1
  13.         valueset=self.options[index]
  14.         self.lastindex=index
  15.     end
  16.     self.valueText:SetText(valueset)
  17.     self:set(valueset)
  18. end
  Reply With Quote
06-05-22, 10:42 AM   #5
nonexistentx_x
A Murloc Raider
 
nonexistentx_x's Avatar
Join Date: Mar 2019
Posts: 8
solved

Now it's fine. Thx so much! =)
  Reply With Quote
06-06-22, 05:09 PM   #6
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,313
Assuming your :get() and :set() functions are working correctly, this is a simpler method.
Lua Code:
  1. ["onclick"]=function(self)
  2.     local newvalue=self.options[Wrap((tIndexOf(self.options,self:get()) or 0))+1,#self.options)];
  3.     self.valueText:SetText(newvalue);
  4.     self:set(newvalue);
  5. end

Note: Both tIndexOf() and Wrap() are Blizzard-defined functions located in SharedXML\TableUtil.lua and SharedXML\MathUtil.lua respectively.
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
  Reply With Quote
06-13-22, 01:42 AM   #7
nonexistentx_x
A Murloc Raider
 
nonexistentx_x's Avatar
Join Date: Mar 2019
Posts: 8
Originally Posted by SDPhantom View Post
Assuming your :get() and :set() functions are working correctly, this is a simpler method.
Lua Code:
  1. ["onclick"]=function(self)
  2.     local newvalue=self.options[Wrap((tIndexOf(self.options,self:get()) or 0))+1,#self.options)];
  3.     self.valueText:SetText(newvalue);
  4.     self:set(newvalue);
  5. end

Note: Both tIndexOf() and Wrap() are Blizzard-defined functions located in SharedXML\TableUtil.lua and SharedXML\MathUtil.lua respectively.
more simple for me:
Lua Code:
  1. function own:getnext(tbl, item)
  2.     local found
  3.     for i = 1, #tbl do
  4.         if found then return tbl[i] end
  5.         found = tbl[i] == item
  6.     end
  7. end
  Reply With Quote
06-13-22, 05:40 AM   #8
Kanegasi
A Molten Giant
 
Kanegasi's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2007
Posts: 666
That's smart, I tend to get a little complicated before I get the code slimmed down.

However, that won't return the first indexed item if you feed it the last one. I adjusted it:

Lua Code:
  1. function own:getnext(tbl, item)
  2.     if tbl[#tbl] == item then
  3.         return tbl[1]
  4.     else
  5.         local found
  6.         for i = 1, #tbl do
  7.             if found then
  8.                 return tbl[i]
  9.             end
  10.             found = tbl[i] == item
  11.         end
  12.     end
  13. end
  Reply With Quote
06-13-22, 05:52 AM   #9
nonexistentx_x
A Murloc Raider
 
nonexistentx_x's Avatar
Join Date: Mar 2019
Posts: 8
Originally Posted by Kanegasi View Post
That's smart, I tend to get a little complicated before I get the code slimmed down.

However, that won't return the first indexed item if you feed it the last one. I adjusted it:

Lua Code:
  1. function own:getnext(tbl, item)
  2.     if tbl[#tbl] == item then
  3.         return tbl[1]
  4.     else
  5.         local found
  6.         for i = 1, #tbl do
  7.             if found then
  8.                 return tbl[i]
  9.             end
  10.             found = tbl[i] == item
  11.         end
  12.     end
  13. end
it returns the first index, just need to write it otherwise:
Lua Code:
  1. local newvalue = self:getnext(self.options, self:get()) or self.options[1];
  Reply With Quote
06-13-22, 06:27 AM   #10
Kanegasi
A Molten Giant
 
Kanegasi's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2007
Posts: 666
Aha, you had it handled. Oh well haha.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » simple table loop

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