| View previous topic :: View next topic |
| Author |
Message |
subarashii Voice
Joined: 18 Mar 2007 Posts: 2
|
Posted: Sun Mar 18, 2007 10:39 am Post subject: string map question |
|
|
Hi there,
I have a question regarding the string map command. What i understand so far: In general you have to set up pairs of words which are replaced by their counterparts. So in this example "not" and "script" is being replaced to "good" and "tcl script".
| Code: | set mytext "i am a not working script"
set nword [string map -nocase {
"not" "good"
"script" "tcl script"
} $mytext];
putmsg $chan "$nword";
.. and so on |
This example works fine. But if i try to insert string variables like lets say:
| Code: |
set string_one "not"
set string_two "good"
set nword [string map -nocase {
$string_one $string_two
} $mytext]; |
Well my noobish version does not work Manual pages says this:
| Quote: | | Replaces substrings in string based on the key-value pairs in mapping. mapping is a list of key value key value ... as in the form returned by array get. |
I tried some variations resulting in not replacing the string or in inbalance char maps. Can someone drop me a quick feedback how to solve this?
tnx in advance |
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Sun Mar 18, 2007 10:59 am Post subject: |
|
|
The reason why the second version does not work, is that you provide the two strings as separate arguments, whereas the documentation clearly specifies that string map expects <map> to be a (single argument) list with value pairs.
Use this instead:
| Code: | | set nword [string map -nocase [list $string_one $string_two] $mytext]; |
I would also suggest using the list-command whenever you build a list from strings; ie:
| Code: | | set nword [string map -nocase [list "not" "good" "script" "tcl script"] $mytext] |
_________________ NML_375, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
subarashii Voice
Joined: 18 Mar 2007 Posts: 2
|
Posted: Sun Mar 18, 2007 1:18 pm Post subject: |
|
|
| tnx a lot. It works now. The list-command was the problem. TCL syntax still confuses me a lot. |
|
| Back to top |
|
 |
Sir_Fz Revered One

Joined: 27 Apr 2003 Posts: 3793 Location: Lebanon
|
Posted: Sun Mar 18, 2007 1:27 pm Post subject: |
|
|
When you create lists using {} everything between the braces won't be evaluated, which means all Tcl commands and variables will be treated as normal strings or elements inside the list. [list] does the same thing (creates a list), but everything after [list] will be evaluated thus you can use variables and commands in it. _________________ Follow me on GitHub
- Opposing
Public Tcl scripts |
|
| Back to top |
|
 |
|