Hi Mathieu, On 2012-01-27 19:06, Mathieu Dupont wrote:
Hi List, Running the following code, variable "a" should not be modified by my function, and stay (1,2), but it does get modified and becomes (4,2) like the new variable "b" I am creating.
Tables are references. Using the “local” keyword, you generate a local variable that still points to the original table. Instead, you need to (deep) copy the table explicitly. ································································· \starttext \startluacode function myFunc(arg) local var = table.copy(arg) var[1] = var[1] + 3 return var end local a = {1,2} local b = myFunc(a) context.type(table.serialize(a)) context[[\par\blank]] context.type(table.serialize(b)) context[[\par\blank]] \stopluacode \stoptext \endinput ································································· There’s also a function table.fastcopy(), see http://wiki.contextgarden.net/table_manipulation#table.fastcopy.28table_t.2C... Hth, Philipp
What is wrong with my code ? Thank you for any hint ! Mathieu
\starttext
\startluacode
function myFunc(arg) local var = arg var[1] = var[1] + 3 return var end
local a = {1,2}
context("a = \\{") context(a[1]) context(",\\;") context(a[2]) context("\\}\\par") context("\\blank")
local b = myFunc(a)
context("a = \\{") context(a[1]) context(",\\;") context(a[2]) context("\\}\\par") context("\\blank")
context("b = \\{") context(b[1]) context(",\\;") context(b[2]) context("\\}\\par")
\stopluacode
\stoptext
___________________________________________________________________________________ If your question is of interest to others as well, please add an entry to the Wiki!
maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context webpage : http://www.pragma-ade.nl / http://tex.aanhet.net archive : http://foundry.supelec.fr/projects/contextrev/ wiki : http://contextgarden.net ___________________________________________________________________________________
-- () ascii ribbon campaign - against html e-mail /\ www.asciiribbon.org - against proprietary attachments