是否有 lmap 的破坏性版本

问题描述 投票:0回答:1

我想运行 lmap,但是是破坏性的,即在 elist 本身上。我当然可以用

set listy [ lmap x $listy { func1 x }  ]

但是还有别的吗?
谢谢。

list tcl in-place
1个回答
0
投票

没有内置命令。你需要做一些循环:

set items {q w e r t y u i o p}

# iterate over the indices
for {set idx 0} {$idx < [llength $items]} {incr idx} {
    # get the item
    set item [lindex $items $idx]

    # generate an updated item
    set item [string toupper $item]

    # put the updated item back
    lset items $idx $item
}

puts $items

请注意,值是概念上写时复制(尽管幕后实现比这更有效);在运行循环之前拥有该列表的任何内容都不会看到更新,除非它们重新读取包含该列表的变量。这在实践中比在脚下改变值更好(更低的错误率)......

© www.soinside.com 2019 - 2024. All rights reserved.