ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* LuaTeX Function Arguments Variables Parameters List ConTeXt Lua \startluacode
       [not found] <COL107-W487CDE8763FF99FDA17729B28F0@phx.gbl>
@ 2012-01-28  0:06 ` Mathieu Dupont
  2012-01-28  0:39   ` Philipp Gesang
  2012-01-28  3:01   ` Mathieu Dupont
  0 siblings, 2 replies; 3+ messages in thread
From: Mathieu Dupont @ 2012-01-28  0:06 UTC (permalink / raw)
  To: ConTeXt


[-- Attachment #1.1: Type: text/plain, Size: 1112 bytes --]


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





 		 	   		   		 	   		  

[-- Attachment #1.2: Type: text/html, Size: 3085 bytes --]

[-- Attachment #2: Type: text/plain, Size: 485 bytes --]

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

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: LuaTeX Function Arguments Variables Parameters List ConTeXt Lua \startluacode
  2012-01-28  0:06 ` LuaTeX Function Arguments Variables Parameters List ConTeXt Lua \startluacode Mathieu Dupont
@ 2012-01-28  0:39   ` Philipp Gesang
  2012-01-28  3:01   ` Mathieu Dupont
  1 sibling, 0 replies; 3+ messages in thread
From: Philipp Gesang @ 2012-01-28  0:39 UTC (permalink / raw)
  To: mailing list for ConTeXt users


[-- Attachment #1.1: Type: text/plain, Size: 2529 bytes --]

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_.5Bboolean_meta.5D.29>

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

[-- Attachment #1.2: Type: application/pgp-signature, Size: 198 bytes --]

[-- Attachment #2: Type: text/plain, Size: 485 bytes --]

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

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: LuaTeX Function Arguments Variables Parameters List ConTeXt Lua \startluacode
  2012-01-28  0:06 ` LuaTeX Function Arguments Variables Parameters List ConTeXt Lua \startluacode Mathieu Dupont
  2012-01-28  0:39   ` Philipp Gesang
@ 2012-01-28  3:01   ` Mathieu Dupont
  1 sibling, 0 replies; 3+ messages in thread
From: Mathieu Dupont @ 2012-01-28  3:01 UTC (permalink / raw)
  To: ConTeXt


[-- Attachment #1.1: Type: text/plain, Size: 217 bytes --]


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
 		 	   		   		 	   		   		 	   		  

[-- Attachment #1.2: Type: text/html, Size: 738 bytes --]

[-- Attachment #2: Type: text/plain, Size: 485 bytes --]

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

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2012-01-28  3:01 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <COL107-W487CDE8763FF99FDA17729B28F0@phx.gbl>
2012-01-28  0:06 ` LuaTeX Function Arguments Variables Parameters List ConTeXt Lua \startluacode Mathieu Dupont
2012-01-28  0:39   ` Philipp Gesang
2012-01-28  3:01   ` Mathieu Dupont

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).