This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

string map question

Help for those learning Tcl or writing their own scripts.
Post Reply
s
subarashii
Voice
Posts: 2
Joined: Sun Mar 18, 2007 10:14 am

string map question

Post by subarashii »

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: Select all

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: Select all

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 :P Manual pages says this:
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
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

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: Select all

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: Select all

set nword [string map -nocase [list "not" "good" "script" "tcl script"] $mytext]
NML_375
s
subarashii
Voice
Posts: 2
Joined: Sun Mar 18, 2007 10:14 am

Post by subarashii »

tnx a lot. It works now. The list-command was the problem. TCL syntax still confuses me a lot.
User avatar
Sir_Fz
Revered One
Posts: 3794
Joined: Sun Apr 27, 2003 3:10 pm
Location: Lebanon
Contact:

Post by Sir_Fz »

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.
  • does the same thing (creates a list), but everything after
    • will be evaluated thus you can use variables and commands in it.
Post Reply