The improper thing is that you are handling a string as if it was a list. You can craft strings that tcl manages to interpret as lists, though it still is a string. For trivial list elements, this is fairly easy, but simply adding a space into the element makes it non-trivial. If you're not careful, it'll catch you offguard, giving you a nice headache.. Or if you use this with a Windows-style filesystem (using \ instead of /}).
You can indeed use split to convert a string into a proper list, and using lindex on a string is technically the same improper issue. However, "split $text" would often be used when the content of $text comes from a remote user, where you have no control what-so-ever of it's original content (hence you cannot quarantee it to be a valid list structure).
Actually, that's quite a good example how "sloppy" coding might sneak up and stab you in the back.. You get comfortable using something like this:
Code: Select all
set keywords "hello dolly"
foreach key $keywords {
bind msg - $key msg:hello
}
This is not proper, but since we're dealing with trivial elements, we get away with it - in fact, if we didn't read the manpages for foreach, we could come to believe that this will add a binding for each word of $keywords. We might even use it when writing a script to add multiple users to the bots userlist:
Code: Select all
proc addusers {handle idx text} {
foreach user $text {
if {[adduser $user [hostmask $user![getchanhost $user]]]} {
setuser $user PASS $user
}
}
}
It works well in a few simple tests... then you want to add someone named {badboy{ ... Now it doesn't work anymore, and you don't understand why it fails all of a sudden...
The solution here, is to use split to convert the string into a proper list.
To put it very simple, the difference between your code and mine, is that I use the list command to let tcl build a list, while you craft something resembling a list well enough for tcl to understand it.