ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
From: "Procházka Lukáš Ing." <LPr@pontex.cz>
To: "mailing list for ConTeXt users" <ntg-context@ntg.nl>
Subject: Re: about table operation in lua-ConTeXt
Date: Mon, 13 Feb 2017 10:22:34 +0100	[thread overview]
Message-ID: <op.yvldnw163h68c7@lprx.pontex.local> (raw)
In-Reply-To: <1869AF30-88A8-4A4F-9959-7E92E9A9DAB4@me.com>

Hello,

tables in Lua are "shared" - there is one copy until you "deep copy" one to another.

Se  comments bellow -

On Mon, 13 Feb 2017 06:50:38 +0100, Jeong Dal <haksan@me.com> wrote:

> Dear all,
>
> It may be a slight off topic.
> In the following example, I copied a table “m” to “tempM”, and remove the last row of tempM.
> Then the output is “ 2, 2”
> I checked the contents of tempM and m and found that they are same even though I didn’t touch m.
> I want to remove the last row of tempM while keeping the table m untouched.
> Please tell me how to do it.
>
> Thank you.
> Best regards,
>
> Dalyoung
>
> \startbuffer[test]
> \startluacode
>  m = {{0, 2, 4},{2, 3 , 4}, {2, 2, -6}}
> local tempM = {} -- OK, 'tempM' and 'm' are different tables - try 'print(tempM, m)' - you'll get two addresses
>
> tempM = m -- Now, 'tempM' and 'm' will point to the same table - "the 'm' table"
-- The original 'tempM' table will be forgotten and removed during next garbage collection as there is no other reference to it
-- ('tempM' starts pointing to 'm', too)
> table.remove(tempM) -- Will remove from one
> context(#tempM, “, ” , #m) -- Obviously - both table point to the same object, try again 'print(tempM, m)'
> \stopluacode
> \stopbuffer
>
> \starttext
> getbuffer[test]
> \stoptext

Best regards,

Lukas


-- 
Ing. Lukáš Procházka | mailto:LPr@pontex.cz
Pontex s. r. o.      | mailto:pontex@pontex.cz | http://www.pontex.cz | IDDS:nrpt3sn
Bezová 1658
147 14 Praha 4

Tel: +420 241 096 751 (+420 720 951 172)
Fax: +420 244 461 038

___________________________________________________________________________________
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://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

  parent reply	other threads:[~2017-02-13  9:22 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <mailman.128.1486929767.14600.ntg-context@ntg.nl>
2017-02-13  5:50 ` Jeong Dal
2017-02-13  8:54   ` Henri Menke
2017-02-13  8:56   ` Hans Hagen
2017-02-13  9:22   ` Procházka Lukáš Ing. [this message]
     [not found] <mailman.1.1486983601.29608.ntg-context@ntg.nl>
2017-02-13 14:03 ` Jeong Dal

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=op.yvldnw163h68c7@lprx.pontex.local \
    --to=lpr@pontex.cz \
    --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).