Thread Tools Display Modes
02-05-17, 09:34 PM   #1
Layback_
An Onyxian Warder
Join Date: Feb 2016
Posts: 358
Accessing value in a same parent table(?)

Hi all,

Just a quick question about dynamic(?) access of an element within a same parent table.

So, let's say I have a table something like this.

Code:
C.size = {
	unitframe = {
		player = {
			width = 253,
			height = 50,
			point = {"RIGHT", UIParent, "CENTER", -150, -220},
		},
		target = {
			width = 253,
			height = 50,
			point = {"LEFT", UIParent, "CENTER", -150, -220},
		},
	},

	portrait = {
		player = {
			size = 50,
			point = {"TOPLEFT", "LUI_Player", "TOPLEFT", 0, 0},
		}
	}
	...
}
For a size of player's portrait (the one with red color), I am willing it to tag along with the size of player's frame height instead of giving it a hard coded value.
(So, in this case I want C.size.portrait.player.size be same as C.size.unitframe.player.height)

For one of the similar cases, I have done the following and it works fine although it doesn't look beautiful.

Code:
C.castbar = {
	player = {
		width = 350,
		height = 30,
		point = {"BOTTOM", UIParent, "BOTTOM", 0, 150},
		iconSize = "height",
		spacing = 1,
	}
}

A.CreateCastbar = function(self)
	local unit = self.unit;

	local iconSize = type(C.castbar[unit].iconSize) == "number" and C.castbar[unit].iconSize or C.castbar[unit][C.castbar[unit].iconSize];
end
Basically, if C.castbar[unit].iconSize is a number it will use its own value, otherwise it will use its height as its value.

Would there be any possible solution for the case above?
  Reply With Quote
02-05-17, 09:49 PM   #2
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
Is there any possible time that it won't be a number?
__________________
"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
02-05-17, 09:58 PM   #3
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,879
Instead of setting C.castbar[unit].iconSize to a string ('height"), leave it as nil to default to the unitframe height:

Lua Code:
  1. local iconSize = C.size.portrait[unit].size or C.size.unitframe[unit].height
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.
  Reply With Quote
02-05-17, 10:02 PM   #4
Layback_
An Onyxian Warder
Join Date: Feb 2016
Posts: 358
Originally Posted by Seerah View Post
Is there any possible time that it won't be a number?
Hi Seerah,

The reason that I gave a string value, "height", to C.castbar.player.iconSize is so it can access to C.castbar.player.height.
(C.castbar[unit][C.castbar[unit].iconSize] == C.castbar[unit][height]).

Apart from this case, yes, there isn't any possible time that it won't be a number and that's what I want
  Reply With Quote
02-05-17, 10:04 PM   #5
Layback_
An Onyxian Warder
Join Date: Feb 2016
Posts: 358
Originally Posted by Fizzlemizz View Post
Instead of setting C.castbar[unit].iconSize to a string ('height"), leave it as nil to default to the unitframe height:

Lua Code:
  1. local iconSize = C.size.portrait[unit].size or C.size.unitframe[unit].height
Hi Fizzlemizz,

You are the best!

That is much simpler than I expected!!!!

+

But would there be any possible ways to access table rather than a) table["a"]["b"] or b) table.a.b ?

Last edited by Layback_ : 02-05-17 at 10:07 PM.
  Reply With Quote
02-05-17, 10:09 PM   #6
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,879
No. Table keys don't have parents like widgets do assuming that's what you a thinking.
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 02-05-17 at 10:12 PM.
  Reply With Quote
02-05-17, 10:15 PM   #7
Layback_
An Onyxian Warder
Join Date: Feb 2016
Posts: 358
Originally Posted by Fizzlemizz View Post
No. Table keys don't have parents like widgets do assuming that's what you a thinking.
That's bit sad

But, thank you for the simple solution !!
  Reply With Quote
02-05-17, 10:27 PM   #8
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,879
What I said was not entirely correct. You can shorten table calls by setting a local variable to the nearest common parent key. In this case,

Lua Code:
  1. local size = C.size
  2. local iconSize = size.portrait[unit].size or size.unitframe[unit].height

It can be useful for repetative tasks as well.
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 02-05-17 at 10:41 PM.
  Reply With Quote
02-05-17, 10:32 PM   #9
Layback_
An Onyxian Warder
Join Date: Feb 2016
Posts: 358
Originally Posted by Fizzlemizz View Post
What I said was not entierly correct. You can shorten table calls by setting a local variable to the nearest common parent key. In this case,

Lua Code:
  1. local size = C.size
  2. local iconSize = size.portrait[unit].size or size.unitframe[unit].height

It can be useful for repetative tasks as well.
So, in that case the key, size, at the very last (before "or") won't be affected by local variable size?

Am I getting it correctly?
  Reply With Quote
02-05-17, 10:35 PM   #10
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,879
Essentially the local size variable is a pointer (reference) to the C.size key creating a shortcut to reaching that key. Everything under it remains the same as calling C.size.xxx.yyy.

You could just as easily:

Code:
    local yahoo = C.size
    local iconSize = yahoo.portrait[unit].size or yahoo.unitframe[unit].height
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 02-05-17 at 10:39 PM.
  Reply With Quote
02-05-17, 10:58 PM   #11
Layback_
An Onyxian Warder
Join Date: Feb 2016
Posts: 358
Originally Posted by Fizzlemizz View Post
Essentially the local size variable is a pointer (reference) to the C.size key creating a shortcut to reaching that key. Everything under it remains the same as calling C.size.xxx.yyy.

You could just as easily:

Code:
    local yahoo = C.size
    local iconSize = yahoo.portrait[unit].size or yahoo.unitframe[unit].height
Thanks for the clarification.

One last question here.

Would it be possible to iterate through dynamic table like:

Code:
C.test = {
    A = {
        "AA",
        "AB",
        "AC",
    },
    B = {
        "BA",
        BB = {
            "BBA",
            "BBB",
        },
    },
    C = {
        CA = {
            "CAA",
            "CAB",
            "CAC",
        },
        "CB",
        CC = {
            "CCA",
        },
    }
}
and print each of their literal values?

I could think of some recursive function, but ain't sure it could work well

Last edited by Layback_ : 02-05-17 at 11:04 PM.
  Reply With Quote
02-05-17, 11:18 PM   #12
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,879
Recursion is fine. To print an unordered list:
Lua Code:
  1. local C = {
  2.     test = {
  3.         A = {
  4.             "AA",
  5.             "AB",
  6.             "AC",
  7.         },
  8.         B = {
  9.             "BA",
  10.             BB = {
  11.                 "BBA",
  12.                 "BBB",
  13.             },
  14.         },
  15.         C = {
  16.             CA = {
  17.                 "CAA",
  18.                 "CAB",
  19.                 "CAC",
  20.             },
  21.             "CB",
  22.             CC = {
  23.                 "CCA",
  24.             },
  25.         }
  26.     }
  27. }
  28.  
  29. local function PrintTable(src)
  30.     for key, value in pairs(src) do
  31.         if type(value) == "table" then
  32.             print(key)
  33.             PrintTable(value)
  34.         else
  35.             print("   ", key, value)
  36.         end
  37.     end
  38. end
  39.  
  40. PrintTable(C)
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 02-05-17 at 11:23 PM.
  Reply With Quote
02-06-17, 01:26 AM   #13
Layback_
An Onyxian Warder
Join Date: Feb 2016
Posts: 358
Originally Posted by Fizzlemizz View Post
Recursion is fine. To print an unordered list:
Thanks a lot Fizzlemizz !!
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Accessing value in a same parent table(?)


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