[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Function returns table
- From: David Given <dg@...>
- Date: Tue, 11 Sep 2007 22:49:38 +0100
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Jeff Wise wrote:
[...]
>>From my diagnostic messages inside the function, r_table contains the
> expected data, but data_table (in the mainline) does not. Is the assignment
> statement transferring all values from the function to the mainline?
Your supplied code should work --- something else must be going wrong. Can you
give more context?
One thing to bear in mind: the only thing the assignment is transferring is a
pointer. Let me expand:
function mt()
local t = {} -- creates a new table, t points at it
t.foo = 1 -- alters table
return t -- returns pointer to table
end
t1 = mt() -- t1 now points at the table mt() created
t2 = t1 -- t2 now points at the *same* table
That is, the table is being created in exactly one place, and then you're just
manipulating pointers to the table. If you call mt() multiple times, each time
it'll create a new, distinct, table. If you alter t1.foo, you'll notice that
t2.foo has changed too, because t1 and t2 are both pointing at the same table.
[...]
> CONFIDENTIALITY NOTICE: This E-mail message and all attachments, which
> originated from Sealy Management Company Inc...
You might want to do something about that...
- --
┌── dg@cowlark.com ─── http://www.cowlark.com ───────────────────
│
│ "There does not now, nor will there ever, exist a programming language in
│ which it is the least bit hard to write bad programs." --- Flon's Axiom
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iD8DBQFG5w1yf9E0noFvlzgRAh2VAJ0QvQ+OVqO6E3xfZ8Q7aTMCSdTdLACeMD5q
zojNRlhgUE780rnoplOUq6c=
=gL+N
-----END PGP SIGNATURE-----