| View previous topic :: View next topic |
| Author |
Message |
NoZparker Voice
Joined: 16 Feb 2004 Posts: 34
|
Posted: Mon Sep 28, 2009 8:06 pm Post subject: Commands |
|
|
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. |
|
| Back to top |
|
 |
arfer Master

Joined: 26 Nov 2004 Posts: 436 Location: Manchester, UK
|
|
| Back to top |
|
 |
NoZparker Voice
Joined: 16 Feb 2004 Posts: 34
|
Posted: Mon Sep 28, 2009 10:12 pm Post subject: How to add text to a line of text |
|
|
many thanks for your quick reply
| Code: | | set text "[join [lrange [split $text] 2 end]] requested by $nick" |
does work
I have also found that
| Code: | 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. |
|
| Back to top |
|
 |
arfer Master

Joined: 26 Nov 2004 Posts: 436 Location: Manchester, UK
|
Posted: Mon Sep 28, 2009 10:31 pm Post subject: |
|
|
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 |
|
| Back to top |
|
 |
NoZparker Voice
Joined: 16 Feb 2004 Posts: 34
|
Posted: Mon Sep 28, 2009 10:42 pm Post subject: |
|
|
whilst reading the url http://eggdrop.org.ru/data/man/sunnet/index.html
i've found another method (so easy)
| Code: |
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. |
|
| Back to top |
|
 |
arfer Master

Joined: 26 Nov 2004 Posts: 436 Location: Manchester, UK
|
Posted: Tue Sep 29, 2009 10:24 am Post subject: |
|
|
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: |
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: |
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: |
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: |
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: |
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: |
set text [lrange [split $text] 2 end]
lappend text :requested by $nick
|
| Code: |
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 |
|
| Back to top |
|
 |
|