Thursday, October 16, 2008

String Map command.. is there any bug..?

String map is widely used command. It helps user to manipulates many operations.
The syntax is as follows- string option arg ?arg ...?
For more details u can refer following link.
http://www.tcl.tk/man/tcl8.4/TclCmd/string.htm

I was using this string map to replace a string say 251 in following code from set of strings (contents of a).
My code was as follows-
set a "251 587 569 485"
set b "251"
set c [string map {$b " "} $a]
after executing the above set of lines i was expecting to get the following result-
i.e. content of variable c must change to " 587 569 485".
But it did not work. because the in string map variable reference is not working.
If i change the code as follows , then it will work-
set a "251 587 569 485"
set c [string map { 251 " "} $a]
Now the content of variable c will be " 587 569 485".
I don't know weather it is a bug or not.. :(

I approached other method to over come this problem.
I wrote following code
set a "251 587 569 485"
set b "251"
regexp -indices " $b " $a var;
c [string replace $a [lindex $var 0] [lindex $var 1] " "]
The above set of code replaces the variable b from variable a with null charecter. :)

2 comments:

  1. Hi Manjunath,

    Variable substitution is not performed on words enclosed in braces. It might be the reason that string map is not substituting properly.

    ReplyDelete
  2. Yes Sowmya that might be the reason...

    ReplyDelete