egghelp.org community Forum Index
[ egghelp.org home | forum home ]
egghelp.org community
Discussion of eggdrop bots, shell accounts and tcl scripts.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Spliting a message into words

 
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help
View previous topic :: View next topic  
Author Message
Fill
Halfop


Joined: 18 Jan 2009
Posts: 80

PostPosted: Tue Apr 07, 2009 5:55 pm    Post subject: Spliting a message into words Reply with quote

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? Laughing
Back to top
View user's profile Send private message
nml375
Revered One


Joined: 04 Aug 2006
Posts: 2857

PostPosted: Tue Apr 07, 2009 6:08 pm    Post subject: Reply with quote

Something like this?
Code:
..
foreach word [split $text] {
 puthelp "PRIVMSG #yourchan :$word"
}

_________________
NML_375, idling at #eggdrop@IrcNET
Back to top
View user's profile Send private message
raider2k
Op


Joined: 01 Jan 2008
Posts: 140

PostPosted: Tue Apr 07, 2009 7:49 pm    Post subject: Reply with quote

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
View user's profile Send private message
Way2Death
Voice


Joined: 31 Mar 2009
Posts: 15

PostPosted: Tue Apr 07, 2009 8:13 pm    Post subject: Reply with quote

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:
set fline ""


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
View user's profile Send private message
arfer
Master


Joined: 26 Nov 2004
Posts: 436
Location: Manchester, UK

PostPosted: Tue Apr 07, 2009 8:28 pm    Post subject: Reply with quote

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
View user's profile Send private message
raider2k
Op


Joined: 01 Jan 2008
Posts: 140

PostPosted: Tue Apr 07, 2009 9:49 pm    Post subject: Reply with quote

thank you,
wasnt sure if join does what it the name says ^^
Back to top
View user's profile Send private message
Fill
Halfop


Joined: 18 Jan 2009
Posts: 80

PostPosted: Wed Apr 08, 2009 3:33 am    Post subject: Reply with quote

Thanks, worked perfectly Wink
Back to top
View user's profile Send private message
arfer
Master


Joined: 26 Nov 2004
Posts: 436
Location: Manchester, UK

PostPosted: Wed Apr 08, 2009 10:14 am    Post subject: Reply with quote

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
View user's profile Send private message
Fill
Halfop


Joined: 18 Jan 2009
Posts: 80

PostPosted: Sat Apr 11, 2009 1:51 pm    Post subject: Reply with quote

wouldn't a [list $text] solve the problem?
Back to top
View user's profile Send private message
nml375
Revered One


Joined: 04 Aug 2006
Posts: 2857

PostPosted: Sat Apr 11, 2009 5:07 pm    Post subject: Reply with quote

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
View user's profile Send private message
Fill
Halfop


Joined: 18 Jan 2009
Posts: 80

PostPosted: Sun Apr 12, 2009 4:13 am    Post subject: Reply with quote

ahmm, I get it... thanks for the great help
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help All times are GMT - 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Forum hosting provided by Reverse.net

Powered by phpBB © 2001, 2005 phpBB Group
subGreen style by ktauber