|
Joined: Aug 2006
Posts: 7
Nutrimatic drinks dispenser
|
OP
Nutrimatic drinks dispenser
Joined: Aug 2006
Posts: 7 |
have switch..case in mirc?? i hope to see it on the next version.
|
|
|
|
Joined: Aug 2004
Posts: 7,252
Hoopy frood
|
Hoopy frood
Joined: Aug 2004
Posts: 7,252 |
Clarification would be helpful. Also if this is a suggestion for a future version, then it should be in the Suggested Features forum, not Connection Issues
|
|
|
|
Joined: Aug 2006
Posts: 7
Nutrimatic drinks dispenser
|
OP
Nutrimatic drinks dispenser
Joined: Aug 2006
Posts: 7 |
i'm sorry
|
|
|
|
Joined: Sep 2005
Posts: 2,881
Hoopy frood
|
Hoopy frood
Joined: Sep 2005
Posts: 2,881 |
A switch/case statement in mIRC syntax would be like this (if it followed C): switch ($time(HH:nn)) {
case (10:00) {
; $time(HH:nn) is 10:00
break
}
case (11:00) {
; $time(HH:nn) is 11:00
break
}
default {
; $time(HH:nn) is neither of the above
}
; Here is where the script will go to when mIRC hits a break statement.
; Without the break statements mIRC would just fall through to the next case.
}
|
|
|
|
Joined: Aug 2006
Posts: 7
Nutrimatic drinks dispenser
|
OP
Nutrimatic drinks dispenser
Joined: Aug 2006
Posts: 7 |
|
|
|
|
Joined: Aug 2006
Posts: 7
Nutrimatic drinks dispenser
|
OP
Nutrimatic drinks dispenser
Joined: Aug 2006
Posts: 7 |
ON *:JOIN:*:{ switch ($time(HH:nn)) { case (19:50) { echo -a 4 $time(HH:nn) is 19:50 break } case (19:51) { echo -a 7 $time(HH:nn) is 19:51 break } default { echo -a $time(HH:nn) } } } output: SWITCH Unknown command - CASE Unknown command - CASE Unknown command - DEFAULT Unknown command
|
|
|
|
Joined: Aug 2004
Posts: 7,252
Hoopy frood
|
Hoopy frood
Joined: Aug 2004
Posts: 7,252 |
|
|
|
|
Joined: Aug 2006
Posts: 7
Nutrimatic drinks dispenser
|
OP
Nutrimatic drinks dispenser
Joined: Aug 2006
Posts: 7 |
ohhh i see
|
|
|
|
Joined: Aug 2004
Posts: 7,252
Hoopy frood
|
Hoopy frood
Joined: Aug 2004
Posts: 7,252 |
If you would clarify just what switch case does, as I'm unfamiliar with the term from a programming perspective, something might be coded, however, until I know just what it's supposed to do, I can't even start thinking about it, realistically.
|
|
|
|
Joined: Sep 2005
Posts: 2,881
Hoopy frood
|
Hoopy frood
Joined: Sep 2005
Posts: 2,881 |
I explained it above. Read the comments.
|
|
|
|
Joined: Oct 2005
Posts: 1,741
Hoopy frood
|
Hoopy frood
Joined: Oct 2005
Posts: 1,741 |
|
|
|
|
Joined: Oct 2004
Posts: 73
Babel fish
|
Babel fish
Joined: Oct 2004
Posts: 73 |
Here's a pseudo switch thingy for giving time on join. Note: it's untested and I probably wouldn't suggest coding this way: ON ^*:JOIN:*:echo $color(info) $chan $nick joined $chan at $switch($time(h:n:tt)) | haltdef
alias switch {
tokenize 58 $1
goto $iif((($2) && ($isid) && ($1 isnum 1-12) && ($2 isnum 0-59)),$iif($istok(0 15 30 45,$2,32),$2,$iif($2 < 30,past,to)),error)
:0
return exactly $1 o'clock $3
:15
return a quarter past $1 $3
:30
return half past $1 $3
:past
return $2 $iif($2 == 1,minute,minutes) past $1 $3
:45
return a quarter to $iif($1 == 12,1 $3,$calc($1 + 1) $iif($3,$iif($1 == 11,$iif($3 == am,pm,am),$3)))
:to
return $calc(60 - $2) $iif($2 == 59,minute,minutes) to $iif($1 == 12,1 $3,$calc($1 + 1) $iif($3,$iif($1 == 11,$iif($3 == am,pm,am),$3)))
:error
echo $color(wallops) -a SWITCH statement called incorrectly
} Edit: correcting typo's caused double-spacing
Last edited by Mardeg; 11/08/06 11:24 AM.
|
|
|
|
Joined: Mar 2004
Posts: 210
Fjord artisan
|
Fjord artisan
Joined: Mar 2004
Posts: 210 |
I probably wouldn't suggest coding this way: While it propably won't do what SWITCH does in most languages (run a lot faster than a nested If-ElseIf-End If) it's a lot easier to read. Nice coding.
|
|
|
|
Joined: Dec 2002
Posts: 580
Fjord artisan
|
Fjord artisan
Joined: Dec 2002
Posts: 580 |
I probably wouldn't suggest coding this way: While it propably won't do what SWITCH does in most languages (run a lot faster than a nested If-ElseIf-End If) it's a lot easier to read. Nice coding. While in C (or other compiled language with switch), there are a couple advantages to using switch, it is not as likely possible to use a switch in mIRC and get those advantages since it is a scripting language. The only point of adding is would be "for looks". BTW, I wouldn't say "runs a lot faster" above, I'd say it "run somewhat faster (only for compiled languages)". You could also script this to some point...
alias switch { set %~SwitchEval $1 | return $null }
alias case { return $iif($1 == %~SwitchEval, $true, $false) }
alias case-switch-test {
var %Test 1
$switch(%Test) {
if ($case(1)) { echo -s Testing 1 }
if ($case(2)) { echo -s Testing 2 }
}
}
|
|
|
|
Joined: Dec 2002
Posts: 2,962
Hoopy frood
|
Hoopy frood
Joined: Dec 2002
Posts: 2,962 |
A switch case would run considerably faster in mIRC script than if/else's because it would evaluate the condition only once, compared to evaluating it up to n times and an additional variable being evaluated n-1 times aswell using the most efficient if/else equivalent (where n is the number of cases). The goto method can just about match the efficiency of a switch statement for simple situations but it is deeply flawed and can cause major issues if you intend to use error handling (not to mention it allows only a single "switch-equivalent" in each scope).
Your example code doesn't allow for cascading which is a defining feature of switch statements.
Spelling mistakes, grammatical errors, and stupid comments are intentional.
|
|
|
|
Joined: Sep 2003
Posts: 4,230
Hoopy frood
|
Hoopy frood
Joined: Sep 2003
Posts: 4,230 |
This is just the same arguement we all went over ages ago.
Sure it would be nice, might even pick up speed a bit, but really, is it needed to make the mirc scripting language workable?
|
|
|
|
Joined: Dec 2002
Posts: 2,962
Hoopy frood
|
Hoopy frood
Joined: Dec 2002
Posts: 2,962 |
Sure it's not needed to make it workable, but as I'm sure was pointed out previously also, 90% of the scripting language isn't needed. While loops exist despite being fully emulatable by goto's but they're in the language because they provide greater readability and save the need to use goto (which can often be misused). The same reasoning applies to switch/case statements - arguably even more so since the goto workaround is more complex and error-prone than the goto equivalent of a while loop.
Spelling mistakes, grammatical errors, and stupid comments are intentional.
|
|
|
|
Joined: Sep 2003
Posts: 4,230
Hoopy frood
|
Hoopy frood
Joined: Sep 2003
Posts: 4,230 |
Still its the same thing discussed maybe 6 months ago now isnt it.
|
|
|
|
Joined: Dec 2002
Posts: 2,962
Hoopy frood
|
Hoopy frood
Joined: Dec 2002
Posts: 2,962 |
There's nothing in the thread that was linked to properly showing the (potential) efficiency bonuses of a switch/case statement or the full issues with using goto. All that thread really says is "switch/case would be nice but it's not needed", which is true but not the whole story. Of course there are even older threads about this, but seeing as people very rarely search that far back there's no harm summing it up again.
Spelling mistakes, grammatical errors, and stupid comments are intentional.
|
|
|
|
Joined: Jun 2003
Posts: 12
Pikka bird
|
Pikka bird
Joined: Jun 2003
Posts: 12 |
The speed benefits of using goto can be VERY good. In a dialog callback that would often involve a lot of IF statements it can literally be an ORDER OF MAGNITUDE faster.
mIRC has no intermediate compiled form for aliases and thus must parse each call to an alias on the fly. This is quite slow... I'm guessing that because the parser doesn't have to actually evaluate anything until it finds the :LABEL it just sorta scans along keeping track of {}'s and ()'s and stuff.
To prove this a simple benchmark:
ticks for 10000 iterations: calling empty alias: 438 calling 10 failed empty if statements in a row: 1719 calling 10 failed empty if statements one inside the other: 1766 calling alias with 10 if statements but a goto 10th at the start: 1047
Conclusion: the mIRC parse is clearly doing something differently with the goto statement. 10 if statements with one inside the other SHOULD mean that only the first is evaluated and the others skipped over, but based on the timing above this isn't happening and it's fully parsing everything but just not evaluating it. The goto on the other hand is somehow skipping over stuff quickly looking for the :TAG but yet still obeying {}'s.
Anyway, goto's are definitely faster than IFs provided you can skip executing/parsing a lot of code in the alias such as in dialogs callbacks...
|
|
|
|
|