Path: news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail From: Mark Summerfield Newsgroups: comp.lang.tcl Subject: Re: The best way to copy a list? Date: Wed, 18 Jun 2025 11:59:04 -0000 (UTC) Organization: A noiseless patient Spider Lines: 43 Message-ID: <102u9m8$33tvo$1@dont-email.me> References: <102tpah$2vfsm$1@dont-email.me> <102u86g$33dlm$2@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Injection-Date: Wed, 18 Jun 2025 13:59:04 +0200 (CEST) Injection-Info: dont-email.me; posting-host="2b8500910d796b2cb9e3e8b40273b934"; logging-data="3274744"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/3GUDTv6rQtX5jaIxEHeuO" User-Agent: Pan/0.154 (Izium; 517acf4) Cancel-Lock: sha1:Q9RVlIgUwACtMMl6aBU348WvFko= On Wed, 18 Jun 2025 17:03:36 +0530, Ashok wrote: > Not sure I understand the question. Why not just > > set b $a > > ? > > Also, your code will not always give identical results using expr ([expr > {$x}] is not always $x). For example, > > % set a {a 0x10 b} > a 0x10 b % set b [lmap x $a {expr {$x}}] > a 16 b > > You could use one of the following forms instead > > % set b [lmap x $a {lindex $x}] > a 0x10 b % set b [lmap x $a {string cat $x}] > a 0x10 b % set b [lmap x $a {return -level 0 $x}] > a 0x10 b > > but as I said, why not just set a $b ? > > /Ashok > > set a {a 0x10 b} > On 6/18/2025 12:49 PM, Mark Summerfield wrote: >> Is using `lmap` the best way to copy a list? For example: >> >> package require struct::list 1 set a {a bc def ghij klmno} >> # is the following the best way to copy a list? >> set b [lmap x $a {expr {$x}}] >> puts "a={$a} b={$b} a==b=[struct::list equal $a $b]" >> >> Output: >> >> a={a bc def ghij klmno} b={a bc def ghij klmno} a==b=1 I'm coming to Tcl from Python & Go. In Python saying `a = [1, 2, 3]` and then `b = a` makes `b` a _reference_ to `a` rather than an independent variable. Now I realise that Tcl doesn't do it that way and that `set a $b` works fine.