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
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
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.
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.
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.
Mark Summerfield <m.n.summerfield@gmail.com> posted:
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.
When I had to do some Python for work, after being familiar with Tcl,
I was quite shocked to find that Python shares values in that way! 😲
Sysop: | DaiTengu |
---|---|
Location: | Appleton, WI |
Users: | 1,064 |
Nodes: | 10 (0 / 10) |
Uptime: | 170:43:10 |
Calls: | 13,692 |
Files: | 186,936 |
D/L today: |
100 files (20,246K bytes) |
Messages: | 2,411,676 |