| |||||||||||
lreplace add extra {} in my result Posted by Dean (Dean), 18 September 2007 hi all,I have many string in a file and they look like something here: string1 : a male "John" "city" string2 : b male "Smith" "city" string3: c "fe" "Mary" "city" Now I want to modify all string which includes "male" and write them to another file, eg: before changing: a male "John" "city" after changing: a male ${!script} "city" So I write below script: set input [open file r+] set output [open file2 w+] set v \${!script} foreach line [split [read $input] \n] { if {[lindex $line 1] eq "male"} { set tmp [lreplace $line 2 2 "XXXX"] regsub {XXXX} $tmp $v tmp puts $result $tmp } The new string is changed to: a male ${!script} {city} It adds extra brackets on the last word. Any solution on the issue please? Thanks. Dean Posted by admin (Graham Ellis), 18 September 2007 You are replacing one element in your list with another list element, and I suspect that the replacement includes a special character such as a leading or trainling space. So when you print out the list $tmp the extra { and } are added to indicatethe scope of the list element with the special character.If you have a look at the join command, you'll find that it provides a mechanism to connect all the elements of a list into a single string; the effect of doing this will be to remove those curly braces for you. Posted by Dean (Dean), 19 September 2007 hi Graham,Thanks for your reply. But the join command can't address below problem: orinigal string: a male "${John}" "${Roma}" I want to change it to: a male ${!script} "${Roma}" I excute : set tv \${!script} set tmp [join [lreplace $line 5 5 $tv]] puts $result $tmp I get : a male ${!script} ${Roma} We are almost successful except we lose the double quotation marks of the last word. how could we remain the quotation for ${Roma}? Thanks! Posted by admin (Graham Ellis), 19 September 2007 Dean, I'm not following the whole story here - your code changes the fifth element of a list but the example shows the second element being changed. And without the whole story, it's very hard for me to suggest a whole answer.I can tell yiou that lreplace treats the string as a list, so any inputs which include quoted strings or backslashes will be transformed ... but of course you can tell me that too as you've seen the problem. If neither a list nor split can lead you easily to what you have to do, then perhaps string commands might be more appropriate. Posted by Dean (Dean), 19 September 2007 hi Graham,thanks for your update and I have fixed the problem now. The whole story is : original string: (in file1) 1.3 aa bb cc dd "${:animal }" "${=:animal %s:}" changing it to: (write into file2) 1.3 aa bb cc dd ${!script } "${=:animal %s:}" ------------------- I use below code ---------------- set tv "\${!script}" set input [open file1 r+] set result [open file2 w+] foreach line [split [read $input] \n] { set last \"[lindex $line 6]\" set tmp [join [lreplace $line 5 6 $tv $last]] puts $result $tmp } I get the correct result by it. I'd like to post it here and maybe you or someone else can find out a better solution Thanks! This page is a thread posted to the opentalk forum
at www.opentalk.org.uk and
archived here for reference. To jump to the archive index please
follow this link.
|
| ||||||||||
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho |