Thread Tools Display Modes
05-08-07, 08:06 PM   #1
Riraito
An Aku'mai Servant
 
Riraito's Avatar
Join Date: Dec 2006
Posts: 32
table inheritance?

is there any way to get a table to inherit the value(s) of another table?
for example if I have:
a = {1,2,4}

is there any way to create a new table b, which inherits all the values of a, without having to do each value individually ( b = { a[1], a[2], a[3] } )
__________________
  Reply With Quote
05-08-07, 08:43 PM   #2
Tekkub
A Molten Giant
 
Tekkub's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2005
Posts: 960
you can do a deep copy, or there's some metatabe magics you can use to make it look up a value in a if that value isn't stored in b... I think it would be...

setmetatable(b, {__index = a})

might have that wrong tho, metatables always mess me up.
  Reply With Quote
05-08-07, 08:53 PM   #3
Riraito
An Aku'mai Servant
 
Riraito's Avatar
Join Date: Dec 2006
Posts: 32
Originally Posted by Tekkub
setmetatable(b, {__index = a})
that did it! thanks
__________________
  Reply With Quote
05-08-07, 10:03 PM   #4
Riraito
An Aku'mai Servant
 
Riraito's Avatar
Join Date: Dec 2006
Posts: 32
ok so I tried doing it manually and it seemed to work fine, but when I ran it in my code, it still acts like Im doing a simple a = b; statement, and when I change one table, it changes the other
__________________
  Reply With Quote
05-09-07, 01:43 AM   #5
Shirik
Blasphemer!
Premium Member
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Mar 2007
Posts: 818
Originally Posted by Riraito
ok so I tried doing it manually and it seemed to work fine, but when I ran it in my code, it still acts like Im doing a simple a = b; statement, and when I change one table, it changes the other
a = b doesn't make a copy, it makes a reference. Changing one will change the other.

Depending on what you want to do, tekkub's answer may be workable for you:

b = setmetatable({}, {__index = a})

Alternatively you can copy all of the keys in a for loop. If you just want to copy raw data, however, the quickest solution, for a simple table, will be to use a metatable to reference it until it's changed. For more complicated solutions (specifically handling nested tables), try this code I just made up now (very minimally tested!)....

Code:
-- Generates the metamethod to handle copying data
function _GenMetamethod(table)
   return function(t, k)
      if not rawget(t, k) then
         local v = table[k];
         if not v then return nil; end
         if type(v) == "table" then
            local newval = {};
            setmetatable(newval, {__index = _GenMetamethod(v)});
            return newval;
         else
            rawset(t, k, v);
            return v;
         end
      else
         return rawget(t, k);
      end
   end
end

-- Now we have a table
a = {1,2,3,4,5};
-- We can copy it to b like this
b = {};
setmetatable(b, {__index = _GenMetamethod(a)});
-- Now let's try this...
b[2] = 10;
assert(a[2] == 2 and b[2] == 10);
assert(a[3] == 3 and b[3] == 3);
If you don't understand how or why this works, I recommend checking out Programmign in Lua (PiL) regarding metatables
__________________
たしかにひとつのじだいがおわるのお
ぼくはこのめでみたよ
だけどつぎがじぶんおばんだってことわ
しりたくなかったんだ
It's my turn next.

Shakespeare liked regexes too!
/(bb|[^b]{2})/
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » table inheritance?


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