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.

Commands

Help for those learning Tcl or writing their own scripts.
Post Reply
N
NoZparker
Voice
Posts: 34
Joined: Mon Feb 16, 2004 6:07 am

Commands

Post by NoZparker »

Where can i find a list of ALL commands and the correct syntax used in a tcl
for example:-
lindex
lrange
lreplace
and how to add a line of text to a line of text
eg,
set text [lrange [split $text] 2 end] + "requested by $nick"
It's times like this I wished I had listened to What my dad used to say. Can't say what it was I never listened.
User avatar
arfer
Master
Posts: 436
Joined: Fri Nov 26, 2004 8:45 pm
Location: Manchester, UK

Post by arfer »

This thread probably belongs in the 'Scripting Help' forum

Core Tcl 8.4

http://www.tcl.tk/man/tcl8.4/TclCmd/contents.htm

Core Tcl 8.5

http://www.tcl.tk/man/tcl8.5/TclCmd/contents.htm

Eggdrop 1.6.19 Tcl

http://www.eggheads.org/support/egghtml ... mands.html

Basic Eggdrop Tcl Tutorial

http://eggdrop.org.ru/data/man/sunnet/index.html


Your string concatenation could be very simply written :-

Code: Select all

set text "[join [lrange [split $text] 2 end]] requested by $nick"
The statement would be first parsed to make the command and variable substitutions before assigning the result to the variable text.

Exact syntax above assumes you wanted a space between the two components. Note the addition of a 'join' command to convert back to a string.
I must have had nothing to do
N
NoZparker
Voice
Posts: 34
Joined: Mon Feb 16, 2004 6:07 am

How to add text to a line of text

Post by NoZparker »

many thanks for your quick reply

Code: Select all

set text "[join [lrange [split $text] 2 end]] requested by $nick"
does work

I have also found that

Code: Select all

set text [lrange [split $text] 2 end]
lappend text :Requested by $nick
also works
found that merged with other stuff in "tip of the day"
It's times like this I wished I had listened to What my dad used to say. Can't say what it was I never listened.
User avatar
arfer
Master
Posts: 436
Joined: Fri Nov 26, 2004 8:45 pm
Location: Manchester, UK

Post by arfer »

Yes, that would work fine.

However, be careful. In your code, the variable text started life as a string but it is now a list.
I must have had nothing to do
N
NoZparker
Voice
Posts: 34
Joined: Mon Feb 16, 2004 6:07 am

Post by NoZparker »

whilst reading the url http://eggdrop.org.ru/data/man/sunnet/index.html


i've found another method (so easy)

Code: Select all

	set text [lrange [split $text] 2 end]
	set text "$text :requested by $nick"
seems like one minute nothing works, the next minute three options work
It's times like this I wished I had listened to What my dad used to say. Can't say what it was I never listened.
User avatar
arfer
Master
Posts: 436
Joined: Fri Nov 26, 2004 8:45 pm
Location: Manchester, UK

Post by arfer »

It is quite common to find several ways to accomplish the exact same thing. This would be true to some degree of any programming/scripting language.

However, not wishing to seem a bore, I do get the impression that you are not taking in what I am trying to communicate regarding the difference between strings and string lists.

A better understanding of these data structures can save you many problems in the future, though I can say that I do still make such mistakes (such is the ease with which they sometimes get confused).

Taking your latest code example apart :-

Code: Select all

set text [split $text]
Although it cannot be confirmed by simply looking at the statement above, it does give the impression that the original value of the variable text is a string, because it is being split to form a list. The set command overwrites the original string value of text with the list value. Given that it is now a list, you can safely use list commands such as lrange on it :-

Code: Select all

set text [lrange [split $text] 2 end]
Indices for lrange (and other commands) start at zero, so the above statement makes a new list from the 3rd element onwards of text and again overwrites the value of text with this new list. It is STILL a list.

Code: Select all

set text [lrange [split $text] 2 end]
set text "$text :requested by $nick"
This is potentially where things could all go pear shaped. The second statement may well succeed on occasions in generating what you need, yet it does create something that may not have been anticipated. The new list value of text has had string characters attached to it.

There are really two ways to go. Firstly if you want to end up with a normal string :-

Code: Select all

set text [join [lrange [split $text] 2 end]]
set text "$text :requested by $nick"
This is probably the best because it ensures that the value of text is in the same form as it was in the first place. The join command has been used in the first statement to convert the lrange result back into a string. Hence, the second statement simply adds additional characters to form a longer string.

The join command could be alternatively placed as follows :-

Code: Select all

set text [lrange [split $text] 2 end]
set text "[join $text] :requested by $nick"
Secondly, If you definitely need the result to be a list, then you first need to determine if the three words included in ':requested by $nick' are to be three distinct list elements or one single list element. These are the two possibilities :-

Code: Select all

set text [lrange [split $text] 2 end]
lappend text :requested by $nick

Code: Select all

set text [lrange [split $text] 2 end]
lappend text ":requested by $nick"
In reality the problem can get rather more complicated because lists of list and lists of lists of lists etc etc are permitted. That means that the join command wouldn't necessarily create a string. It may create a 'flatter' list. To be fair I would forget this added difficulty until you are confident with the simple case (a normal string OR a flat list of string elements).

Some insight into the sort of problems that can occur can be gained by reading :-

http://www.peterre.info/characters.html
I must have had nothing to do
Post Reply