View previous topic :: View next topic |
Author |
Message |
stdragon Owner

Joined: 23 Sep 2001 Posts: 959
|
Posted: Sun Sep 15, 2002 5:33 pm Post subject: list element followed by "XYZ" instead of space |
|
|
This common error is caused by improper use of list operations on strings. The most common list operations used in this manner are lindex and lrange. Many scripts will use these commands to extract words from a string. For instance:
Code: |
bind pub o|o !kick pub_kick
proc pub_kick {nick uhost hand chan arg} {
# Person to kick is first arg
set target [lindex $arg 0]
# Kick message is remaining words
set kickmsg [lrange $arg 1 end]
...
}
|
However, if you try to kick someone with a strange nick like {hello}kitty, you will soon encounter an error: list element in braces followed by "kitty" instead of space
So, how do you solve this problem? A lot of scripts use an ugly hack (regsub) to add slashes into the string. That is the wrong way. Tcl provides a command called split that splits a string into a proper list. Each word in the string becomes an element in the list.
Therefore the quick fix for this error is to replace $arg with [split $arg] whenever lindex or lrange is used.
For instance, the above script can be fixed like this:
Code: |
bind pub o|o !kick pub_kick
proc pub_kick {nick uhost hand chan arg} {
# Person to kick is first arg
set target [lindex [split $arg] 0]
# Kick message is remaining words
set kickmsg [lrange [split $arg] 1 end]
...
}
|
Now it will work properly with any nick.
If you need more explanation, please post questions below. Not currently possible |
|
Back to top |
|
 |
CoolCold Voice

Joined: 27 Feb 2003 Posts: 9 Location: Moscow,RU
|
Posted: Mon Mar 03, 2003 9:37 pm Post subject: using {} [] etc |
|
|
I think that arguments substitution is one of TCL's features and the same time one of biggest security holes - using {} or [] inproperly,programmer lets intruder to make script work the other way it should...so this short example by stdagon is very impotant I think. |
|
Back to top |
|
 |
tezoost Voice
Joined: 11 Feb 2015 Posts: 1
|
Posted: Wed Feb 11, 2015 6:05 am Post subject: |
|
|
This will show a multi line traceback to the last error. If you read this you will know what you have to do to prevent this error.
The ugly point is, errorInfo shows always the last error. So, if you use catch and a error has been caused it will show that error last, so be fast Wink.
_____________________________________________
Usman |
|
Back to top |
|
 |
|