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.

Spliting a message into words

Help for those learning Tcl or writing their own scripts.
Post Reply
F
Fill
Halfop
Posts: 80
Joined: Sun Jan 18, 2009 6:08 pm

Spliting a message into words

Post by Fill »

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? :lol:
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Something like this?

Code: Select all

..
foreach word [split $text] {
 puthelp "PRIVMSG #yourchan :$word"
}
NML_375
r
raider2k
Op
Posts: 140
Joined: Tue Jan 01, 2008 10:42 am

Post by raider2k »

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
W
Way2Death
Voice
Posts: 15
Joined: Tue Mar 31, 2009 3:30 pm

Post by Way2Death »

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

Code: Select all

set fline ""
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
User avatar
arfer
Master
Posts: 436
Joined: Fri Nov 26, 2004 8:45 pm
Location: Manchester, UK

Post by arfer »

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
r
raider2k
Op
Posts: 140
Joined: Tue Jan 01, 2008 10:42 am

Post by raider2k »

thank you,
wasnt sure if join does what it the name says ^^
F
Fill
Halfop
Posts: 80
Joined: Sun Jan 18, 2009 6:08 pm

Post by Fill »

Thanks, worked perfectly :wink:
User avatar
arfer
Master
Posts: 436
Joined: Fri Nov 26, 2004 8:45 pm
Location: Manchester, UK

Post by arfer »

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
F
Fill
Halfop
Posts: 80
Joined: Sun Jan 18, 2009 6:08 pm

Post by Fill »

wouldn't a [list $text] solve the problem?
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

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
F
Fill
Halfop
Posts: 80
Joined: Sun Jan 18, 2009 6:08 pm

Post by Fill »

ahmm, I get it... thanks for the great help
Post Reply