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.

getting different words/splitting variables up into differen

Old posts that have not been replied to for several years.
Locked
T
TRiNuX

Post by TRiNuX »

Hey.

How can I split a variable or peramiter, that contains more than one word, into different sections?
ex.
set testvar "word1 word2"

how could I get what the first word is and what the second word is from the variable?

do I have to put them into a list and use lindex or somthing?


ta
p
ppslim
Revered One
Posts: 3914
Joined: Sun Sep 23, 2001 8:00 pm
Location: Liverpool, England

Post by ppslim »

Yes and no.

Taking your example, to get the second word from the string, you can use the follwing.
set word [lindex [split $testvar] 1]
OK, so the "set word you can undertsand.

The lindex command is used to get an item out of a list. You have to pass it the list, and the item number you want from the list. The split command is used to convert a string into a list (this is also required to make things safe for later). The 1 is the index within the list that you wish to get. Note the index is 1, but the word we want is word 2, this is because, like in most languages, the first eliment is 0.
T
TRiNuX

Post by TRiNuX »

kewl that works...

another problem (I've seen you give that answer to a previous post about names with { } in them.....

is there a way of removing characters? e.g those { } ones...

I'd assume it would envolve finding the position of the character and then doing somthing to replace it?

I couldn't really figure it out yesterday, could anyone shed some light? ta
p
ppslim
Revered One
Posts: 3914
Joined: Sun Sep 23, 2001 8:00 pm
Location: Liverpool, England

Post by ppslim »

Having {} chars in the text does not matter, so long as you handle the string correctly.

If you use [split] on the text, it should work correctly.
T
TRiNuX

Post by TRiNuX »

K, Having a problem using it.... maybe you could clear it up, or will I HAVE to remove the { for this to work?

using a bind to
bind msg - test handletest

proc handletest { nick host hand args } {
#shows what the output is
putlog "hand [lindex [split $args] 0]"

if {[validuser [lindex [split $args] 0]]} {
puthelp "NOTICE $nick :your handle is ok"

}
return 0
}

/msg bot test HANDLE
works fine, problem is...
/msg bot test HANDLE <anything else here>

returns {handle - thus causing the error and not checking right, so have I got the syntax of the check wrong, or am I going to have to remove the { } brackets?
User avatar
stdragon
Owner
Posts: 959
Joined: Sun Sep 23, 2001 8:00 pm
Contact:

Post by stdragon »

Don't use "args" as the name of your last argument in your procedure. It is a special tcl keyword that creates a list out of the remaining arguments, which adds the extra { and } that you are seeing.

Just rename it to "text" or something else, and it should be fine.
Locked