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.

list element followed by "XYZ" instead of space

Issues often discussed about Tcl scripting. Check before posting a scripting question.
Post Reply
User avatar
stdragon
Owner
Posts: 959
Joined: Sun Sep 23, 2001 8:00 pm
Contact:

list element followed by "XYZ" instead of space

Post by stdragon »

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

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

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
User avatar
CoolCold
Voice
Posts: 9
Joined: Thu Feb 27, 2003 11:29 pm
Location: Moscow,RU

using {} [] etc

Post by CoolCold »

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.
t
tezoost
Voice
Posts: 1
Joined: Wed Feb 11, 2015 6:00 am

Post by tezoost »

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
Post Reply