如何通过TCL拆分字符串并存储在列表中 [英] How to split string and store in list via TCL

查看:76
本文介绍了如何通过TCL拆分字符串并存储在列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法拆分字符串并保存在列表中?如何拆分字符串并保存在两个列表中例如,我有一个字符串,我用 = 分割了几个字符串:

<前>a=1b=2c=3d=4

然后我想创建两个这样的列表 [a,b,c,d][1,2,3,4]:

解决方案

以下是简单的tcl代码

set s "a=1\nb=2\nc=3\nd=4"set s [拆分 $s "\n"]foreach e $s {设置 e [拆分 $e "="]lappend l1 [lindex $e 0]lappend l2 [lindex $e 1]}

现在你有列表 l1 和 [a b c d] 和 l2 有 [1 2 3 4]

Is there a way to split strings and save in a list ? How to split string and save in two list For example, I have a string where I split several string with =:

a=1
b=2
c=3
d=4

and then I want to create two list like this [a,b,c,d] and [1,2,3,4]:

解决方案

Following is a simple tcl code

set s "a=1\nb=2\nc=3\nd=4"
set s [split $s "\n"]
foreach e $s {
    set e [split $e "="]
    lappend l1 [lindex $e 0]
    lappend l2 [lindex $e 1]
}

Now you have list l1 with [a b c d] and l2 has [1 2 3 4]

这篇关于如何通过TCL拆分字符串并存储在列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆