Thread Tools Display Modes
03-27-22, 04:10 AM   #1
Benalish
A Flamescale Wyrmkin
 
Benalish's Avatar
Join Date: Dec 2012
Posts: 123
Print table at specific table depth

I know, maybe a not WoWInterface related question, but I need to manage my addon profile table.

This is my custom function:

Lua Code:
  1. function scope(tbl, depth)
  2.         if depth > 0 then
  3.             for k, v in pairs(tbl) do
  4.                 if type(v) ~= 'table' then
  5.                     print(v)
  6.                 else
  7.                     scope(v, depth - 1)
  8.                 end
  9.             end
  10.         end
  11.     end

This is the usage: let

Lua Code:
  1. stuff = {
  2.         fruit = {
  3.             yellow = {
  4.                 "Banana"
  5.             }, -- depth = 3
  6.             red = {
  7.                 "Apple"
  8.             } -- depth = 3
  9.         },
  10.         city = {
  11.             "Toronto"
  12.         }, -- depth = 2
  13.         name = {
  14.             "Claudia"
  15.         } -- depth = 2
  16.     }
Lua Code:
  1. scope(stuff, 2)

returns

Code:
 
    Toronto
    Claudia
Otherwise,
Lua Code:
  1. scope(stuff, 3)
returns

Code:
    Banana
    Apple
    Toronto
    Claudia
This code works fine for me. Any advice on how to improve it? Maybe insert some code that displays
Lua Code:
  1. nil
if, as here, I specify depth value of 1 or a number greater than 3 (the depth of the table).

Last edited by Benalish : 03-27-22 at 08:18 AM.
  Reply With Quote
03-28-22, 12:17 AM   #2
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,322
I honestly prefer using verbose messages over inserting random nils in where I expect a list. Even if said list is empty. Perhaps if you kept track if the loop has printed anything and at the end, saying "List is empty" if it hasn't printed anything. Using indents, even in your output can give more info on the structure of data being examined too.
__________________
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
03-28-22, 05:27 AM   #3
Benalish
A Flamescale Wyrmkin
 
Benalish's Avatar
Join Date: Dec 2012
Posts: 123
Originally Posted by SDPhantom View Post
I honestly prefer using verbose messages over inserting random nils in where I expect a list. Even if said list is empty. Perhaps if you kept track if the loop has printed anything and at the end, saying "List is empty" if it hasn't printed anything. Using indents, even in your output can give more info on the structure of data being examined too.
Good! What could be a way to detect the maximum depth of the table? In order to display a message stating the maximum depth of the table has been reached.
  Reply With Quote
04-01-22, 12:36 PM   #4
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,322
Let's take your earlier code for example.

Option 1:
(Depth check was moved to before attempting to recursive call)
Code:
	for k, v in pairs(tbl) do
		if type(v) ~= 'table' then
			print(v)
		elseif depth > 1 then
			scope(v, depth - 1)
		else
			print("Maximum depth reached!")
		end
	end
Option 2:
Code:
	if depth > 0 then
		for k, v in pairs(tbl) do
			if type(v) ~= 'table' then
				print(v)
			else
				scope(v, depth - 1)
			end
		end
	else
		print("Maximum depth reached!")
	end
__________________
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
04-02-22, 05:05 AM   #5
Benalish
A Flamescale Wyrmkin
 
Benalish's Avatar
Join Date: Dec 2012
Posts: 123
Thank you very much!
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Print table at specific table depth

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