| View previous topic :: View next topic |
| Author |
Message |
Fill Halfop
Joined: 18 Jan 2009 Posts: 80
|
Posted: Tue Apr 07, 2009 5:55 pm Post subject: Spliting a message into words |
|
|
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?  |
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Tue Apr 07, 2009 6:08 pm Post subject: |
|
|
Something like this?
| Code: | ..
foreach word [split $text] {
puthelp "PRIVMSG #yourchan :$word"
} |
_________________ NML_375, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
raider2k Op
Joined: 01 Jan 2008 Posts: 140
|
Posted: Tue Apr 07, 2009 7:49 pm Post subject: |
|
|
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 |
|
| Back to top |
|
 |
Way2Death Voice
Joined: 31 Mar 2009 Posts: 15
|
Posted: Tue Apr 07, 2009 8:13 pm Post subject: |
|
|
| 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: | 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 |
|
| Back to top |
|
 |
arfer Master

Joined: 26 Nov 2004 Posts: 436 Location: Manchester, UK
|
Posted: Tue Apr 07, 2009 8:28 pm Post subject: |
|
|
| Code: |
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 |
|
| Back to top |
|
 |
raider2k Op
Joined: 01 Jan 2008 Posts: 140
|
Posted: Tue Apr 07, 2009 9:49 pm Post subject: |
|
|
thank you,
wasnt sure if join does what it the name says ^^ |
|
| Back to top |
|
 |
Fill Halfop
Joined: 18 Jan 2009 Posts: 80
|
Posted: Wed Apr 08, 2009 3:33 am Post subject: |
|
|
Thanks, worked perfectly  |
|
| Back to top |
|
 |
arfer Master

Joined: 26 Nov 2004 Posts: 436 Location: Manchester, UK
|
Posted: Wed Apr 08, 2009 10:14 am Post subject: |
|
|
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 |
|
| Back to top |
|
 |
Fill Halfop
Joined: 18 Jan 2009 Posts: 80
|
Posted: Sat Apr 11, 2009 1:51 pm Post subject: |
|
|
| wouldn't a [list $text] solve the problem? |
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Sat Apr 11, 2009 5:07 pm Post subject: |
|
|
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, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
Fill Halfop
Joined: 18 Jan 2009 Posts: 80
|
Posted: Sun Apr 12, 2009 4:13 am Post subject: |
|
|
| ahmm, I get it... thanks for the great help |
|
| Back to top |
|
 |
|