Sujet : Re: The best way to copy a list?
De : m.n.summerfield (at) *nospam* gmail.com (Mark Summerfield)
Groupes : comp.lang.tclDate : 18. Jun 2025, 12:59:04
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <102u9m8$33tvo$1@dont-email.me>
References : 1 2
User-Agent : Pan/0.154 (Izium; 517acf4)
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.