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.
Help for those learning Tcl or writing their own scripts.
Fill
Halfop
Posts: 80 Joined: Sun Jan 18, 2009 6:08 pm
Post
by Fill » Tue Apr 07, 2009 5:55 pm
Hi scripters,
I'm trying to make a script that splits a message into its' words and sends them to the channel.
Example:
<Fill> testing one two three
<Mr.Bot> testing
<Mr.Bot> one
<Mr.Bot> two
<Mr.Bot> three
Any ideas?
nml375
Revered One
Posts: 2860 Joined: Fri Aug 04, 2006 2:09 pm
Post
by nml375 » Tue Apr 07, 2009 6:08 pm
Something like this?
Code: Select all
..
foreach word [split $text] {
puthelp "PRIVMSG #yourchan :$word"
}
NML_375
raider2k
Op
Posts: 140 Joined: Tue Jan 01, 2008 10:42 am
Post
by raider2k » Tue Apr 07, 2009 7:49 pm
if i may join this thread and ask how to join the seperated words then?
exactly the opposite thing should be executed if possible:
foreach word i want it in one line again
Way2Death
Voice
Posts: 15 Joined: Tue Mar 31, 2009 3:30 pm
Post
by Way2Death » Tue Apr 07, 2009 8:13 pm
raider2k wrote: if i may join this thread and ask how to join the seperated words then?
exactly the opposite thing should be executed if possible:
foreach word i want it in one line again
the way i did it was
first set empty string
then i did a for each loop..
depending if its in a file you would do it like:
Code: Select all
set fp [open "thefile.txt" r]
set data [read $fp]
close $fp
set data [split $data "\n"]
foreach line $data{
set fline "$fline $line"
}
i dont know if there is a better way of doing this.. but this works fine for me
so basically loop trough everything, and add every word one by one to the $line string
arfer
Master
Posts: 436 Joined: Fri Nov 26, 2004 8:45 pm
Location: Manchester, UK
Post
by arfer » Tue Apr 07, 2009 8:28 pm
Code: Select all
set fp [open "thefile.txt" r]
set datalist [split [read $fp] \n]
close $fp
set datastring [join $datalist]
Look at the core Tcl commands 'split' and 'join'
I must have had nothing to do
raider2k
Op
Posts: 140 Joined: Tue Jan 01, 2008 10:42 am
Post
by raider2k » Tue Apr 07, 2009 9:49 pm
thank you,
wasnt sure if join does what it the name says ^^
Fill
Halfop
Posts: 80 Joined: Sun Jan 18, 2009 6:08 pm
Post
by Fill » Wed Apr 08, 2009 3:33 am
Thanks, worked perfectly
arfer
Master
Posts: 436 Joined: Fri Nov 26, 2004 8:45 pm
Location: Manchester, UK
Post
by arfer » Wed Apr 08, 2009 10:14 am
Just as an aside, many ppl learning Tcl (me included, countless times) have made mistakes by assuming any text returned to a proc from a bind such as PUB can be treated as a list of words. It isn't. Scripts incorrectly treating this argument as a list will choke on special characters.
The following webpage (dealing with the proper use of split and join commands) is quite well known, and well worth a read.
http://www.peterre.info/characters.html
I must have had nothing to do
Fill
Halfop
Posts: 80 Joined: Sun Jan 18, 2009 6:08 pm
Post
by Fill » Sat Apr 11, 2009 1:51 pm
wouldn't a [list $text] solve the problem?
nml375
Revered One
Posts: 2860 Joined: Fri Aug 04, 2006 2:09 pm
Post
by nml375 » Sat Apr 11, 2009 5:07 pm
Fill:
That depends on what you hope to achieve:
If you are trying to create a list with a single item, containg the whole of $text, that'll work ("lindex [list $list] 0" will return the whole content of $text).
If, however, you are trying to make each word within $text a separate list item, that will not work. Instead, you should be looking at split (Ie: "lindex [split $list] 0" will return the first word in $text).
NML_375
Fill
Halfop
Posts: 80 Joined: Sun Jan 18, 2009 6:08 pm
Post
by Fill » Sun Apr 12, 2009 4:13 am
ahmm, I get it... thanks for the great help