Re: [NTG-context] LuaTeX Function Arguments Variables Parameters List ConTeXt Lua \startluacode

2012-01-27 Thread Mathieu Dupont

Great, thank you very much Philipp.
I will also read this page you pointed and I should understand better Lua then.
http://wiki.contextgarden.net/table_manipulation

Mathieu

  ___
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
___

Re: [NTG-context] LuaTeX Function Arguments Variables Parameters List ConTeXt Lua \startluacode

2012-01-27 Thread Philipp Gesang
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


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


pgpEAMlmRkn38.pgp
Description: PGP signature
___
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
___

[NTG-context] LuaTeX Function Arguments Variables Parameters List ConTeXt Lua \startluacode

2012-01-27 Thread Mathieu Dupont

Hi List,
I am trying to program with Lua but there are concepts I must not understand, 
and the documentation is somewhat lacking.
The only useful thing I could use is Hans' manual, thanks to him.
If someone could help me just understanding the concept I must me missing in 
this simple example I would appreciate it.
So here it is.
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.
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
___