| View previous topic :: View next topic |
| Author |
Message |
arfer Master

Joined: 26 Nov 2004 Posts: 436 Location: Manchester, UK
|
Posted: Wed Feb 22, 2012 3:52 am Post subject: |
|
|
caesar, I think it is possible to use alternative syntax in any programming/scripting languages to achieve the same goal. Nonetheless, thank you for the interesting alternatives.
Out of interest, how would you remove $botnick from the channel list? As far as I can see the lsearch command is there to determine where it is in the list in order to facilitate removal, rather than merely whether or not it is there. _________________ I must have had nothing to do |
|
| Back to top |
|
 |
caesar Mint Rubber

Joined: 14 Oct 2001 Posts: 3741 Location: Mint Factory
|
Posted: Wed Feb 22, 2012 6:07 am Post subject: |
|
|
Sure, but if you can make things run faster at a lower resources consumption, why not?
I've said "proceed to remove it from the list" as in remove the if check statement as it will always return true, not in using another method if that's what you imply.  _________________ Once the game is over, the king and the pawn go back in the same box. |
|
| Back to top |
|
 |
arfer Master

Joined: 26 Nov 2004 Posts: 436 Location: Manchester, UK
|
Posted: Fri Feb 24, 2012 6:17 am Post subject: |
|
|
Thanks caesar, I did understand your intent in posting alternatives. My response was borne of your terminology 'you should use' whereas it might have been better to say 'you could use' or 'consider instead using'. I didn't want other readers to misinterpret your remarks and perhaps believe the script posted by Get_A_Fix is somehow disfunctional. As far as I can tell it is absolutely fine. I certainly hope so, since I contributed to it.
You wrote :-
| Code: |
if {[scan $text %s channel] != 1} {
|
I think you meant :-
| Code: |
if {[scan $text %s channel] == 1} {
|
I suppose you could consider rewriting the existing variable rather than creating another :-
| Code: |
if {[scan $text %s text] == 1} {
|
This is still not exactly equivalent to the existing code. It will not fail if more than one command argument is provided. Rather, it will rewrite the value of text with the first of the arguments, presuming that is what the command user intended. A perfectly fair assumption but I suppose it depends what the author prefers.
You wrote (replacing $channel with $text as per above) :-
| Code: |
if {[string equal -length 1 # $text]} {
|
I like this code very much and might adopt it. Just shows the benefit in looking more closely at command switches/options.
Finally, the 'if' statement is a way of removing $botnick in one uninterrupted statement. If I'm not mistaken it would otherwise require two disparate statements. One to find the list index, then another to remove it. This is not an important point, except if I was to generalize for the benefit of other readers and say that code elements often contain seemingly superfluous statements, yet are deliberate. _________________ I must have had nothing to do |
|
| Back to top |
|
 |
caesar Mint Rubber

Joined: 14 Oct 2001 Posts: 3741 Location: Mint Factory
|
Posted: Fri Feb 24, 2012 3:57 pm Post subject: |
|
|
Actually, it should have been:
| Code: |
if {[scan $text %s channel]} {
|
cos $test is equal with 1 then a if {$test} { true as in equal or bigger than 1 } and if {!$test} { true as in equal with 0 }
To be honest I find scan usefull if for instance I have 3 variables I want to extract from a user input, so with scan is just as simple as:
| Code: |
if {[scan $text %s%s%s first second third] == 3} {
|
that's equal with:
| Code: |
set text [split $text]
if {[llength $text] == 3} {
set first [lindex $text 0]
set second [lindex $text 1]
set third [lindex $text 2]
}
|
notice how easy is with scan to do about the same thing. I'd have to say thanks to user, cos I learned this trick from him.
| Code: |
if {[set bot [lsearch $nicklist $botnick]] != -1} {set nicklist [lreplace $nicklist $bot $bot]}
|
you don't need a if statement as it will always return true. I meant to change it to:
| Code: |
set bot [lsearch $nicklist $botnick]
set nicklist [lreplace $nicklist $bot $bot]
|
_________________ Once the game is over, the king and the pawn go back in the same box. |
|
| Back to top |
|
 |
arfer Master

Joined: 26 Nov 2004 Posts: 436 Location: Manchester, UK
|
Posted: Fri Feb 24, 2012 7:12 pm Post subject: |
|
|
I'm unsure of your code caesar :-
| Code: |
if {[scan $text %s channel]} {
|
If the command user mistakingly fails to input any command arguments, then scan will return -1 yet, when used in this way, any non-zero value is considered true. Hence the code inside the if statement may attempt execution, whereas it should not have. Am I correct in my assessment?
This discussion does give me an idea though. Generally, I concern myself with preventing users accidentally or deliberately inputting things that may break a script. This is what I might use normally :-
| Code: |
set text [regsub -all -- {\s{2,}} [stripcodes bcruag [string trim $text]] " "]
if {[llength [split $text]] == 1} {
|
Quite ugly.
What about this :-
| Code: |
if {[scan [stripcodes bcruag $text] %s%s text dummy] == 1} {
|
Seems to kill several birds with one stone
1. No need to trim $text or convert double spaces between words to one space
2. Fails if the user inputs zero command arguments
3. Fails if the user inputs more than one command argument
4. Is half the number of lines of code
5. Uses scan to make my ugly code more efficient
On the subject of the if/lsearch issue I do think we are still on different wavelengths. I tried to explain that I only used it to construct a single code element, rather than actually determine whether the botnick is present in the list. As you point out, it always is. We both agree that without it, two seperate statements are required. Admittedly, on the subject of code efficiency it may be better to have two seperate statements. _________________ I must have had nothing to do |
|
| Back to top |
|
 |
caesar Mint Rubber

Joined: 14 Oct 2001 Posts: 3741 Location: Mint Factory
|
Posted: Sat Feb 25, 2012 1:42 am Post subject: |
|
|
Oups, my bad. Forgot about -1 return when there's nothing to be scanned.
About:
| Code: |
if {[scan [stripcodes bcruag $text] %s%s text dummy] == 1} {
|
well, if the user will say two or multiple words the scan will return:
| Code: |
% set text "word1 word2 word3"
word1 word2 word3
% scan $text %s%s first second
3
% echo $first
word1
% echo $second
word2
|
notice that it stored in the second variable only the second word, not second and third word. _________________ Once the game is over, the king and the pawn go back in the same box. |
|
| Back to top |
|
 |
|
|
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
|
|