egghelp.org community Forum Index
[ egghelp.org home | forum home ]
egghelp.org community
Discussion of eggdrop bots, shell accounts and tcl scripts.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

string map question

 
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help
View previous topic :: View next topic  
Author Message
subarashii
Voice


Joined: 18 Mar 2007
Posts: 2

PostPosted: Sun Mar 18, 2007 10:39 am    Post subject: string map question Reply with quote

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 Razz 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
View user's profile Send private message
nml375
Revered One


Joined: 04 Aug 2006
Posts: 2857

PostPosted: Sun Mar 18, 2007 10:59 am    Post subject: Reply with quote

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
View user's profile Send private message
subarashii
Voice


Joined: 18 Mar 2007
Posts: 2

PostPosted: Sun Mar 18, 2007 1:18 pm    Post subject: Reply with quote

tnx a lot. It works now. The list-command was the problem. TCL syntax still confuses me a lot.
Back to top
View user's profile Send private message
Sir_Fz
Revered One


Joined: 27 Apr 2003
Posts: 3793
Location: Lebanon

PostPosted: Sun Mar 18, 2007 1:27 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help All times are GMT - 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Forum hosting provided by Reverse.net

Powered by phpBB © 2001, 2005 phpBB Group
subGreen style by ktauber