2016-11-20 32 views
-1

如何搜索每个字符串单词并将其更改为如下所示;如何使用替换

set firstLibrary {12345} 
set secondLibrary {aghij} 

备注:

 
1=a ; 2=g ; 3=h ; 4=i ; 5=j 
set theString {44168} 

然后输出; 44168 = iia68

+0

这个问题不清楚,请添加更多细节 – georoot

+2

看看[字符串映射(http://www.tcl.tk/man/tcl8.6/TclCmd/string.htm#M34)在文档 –

回答

3

您需要做的是将这两个库组装成一个可与string map一起使用的映射。关键在于你可以使用foreach的双列表形式,并且如果分隔符集为空,则split命令可以轻松地将字符串分解为其组成字符。

set map {} 
foreach from [split $firstLibrary ""] to [split $secondLibrary ""] { 
    lappend map $from $to 
} 

应用与string map地图上$theString和打印结果留作练习。

+0

其很好。还有,如何从sttring中分割一个字符串两个字。例如:{helloworlds}和输出结果如下:他将会运行或者ld s – Andre

+0

@Andre这将是正则表达式的一部分:'regexp -all -inline {..?}“helloworlds” –

0

如何获得第二个字符串值,如果第一个相同的字符串值,然后计算平均值如下;

set first {A B B C D E E E E E G K} 
set second {12 42 51 66 24 75 33 11 22 86 43 66} 

set lenghtString [lenght $first] 

for {set i 0} {$i < $lenghtString} {incr i} { 
#arg please 
#If same string on first string then get second string 
    #B B = (42+51)/2 = 46.5 
    #E E E E E = (75+33+11+22+86)/5 = 45.4 
} 

output with puts by rows: 12 46.5 66 24 45.4 86 66