From: Philipp Gesang <gesang@stud.uni-heidelberg.de>
To: mailing list for ConTeXt users <ntg-context@ntg.nl>
Subject: Re: LuaTeX Function Arguments Variables Parameters List ConTeXt Lua \startluacode
Date: Sat, 28 Jan 2012 01:39:29 +0100 [thread overview]
Message-ID: <20120128003929.GA1649@phlegethon> (raw)
In-Reply-To: <COL107-W303DA2C39276D4E046CEF1B28F0@phx.gbl>
[-- 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
___________________________________________________________________________________
next prev parent reply other threads:[~2012-01-28 0:39 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <COL107-W487CDE8763FF99FDA17729B28F0@phx.gbl>
2012-01-28 0:06 ` Mathieu Dupont
2012-01-28 0:39 ` Philipp Gesang [this message]
2012-01-28 3:01 ` Mathieu Dupont
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20120128003929.GA1649@phlegethon \
--to=gesang@stud.uni-heidelberg.de \
--cc=ntg-context@ntg.nl \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).