mIRC Homepage
Posted By: wongpratan Switch Case?? - 06/08/06 12:34 AM
have switch..case in mirc??

i hope to see it on the next version. wink
Posted By: RusselB Re: Switch Case?? - 06/08/06 01:11 AM
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
Posted By: wongpratan Re: Switch Case?? - 06/08/06 02:23 AM
i'm sorry confused
Posted By: hixxy Re: Switch Case?? - 06/08/06 09:49 AM
A switch/case statement in mIRC syntax would be like this (if it followed C):

Code:
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.
}
Posted By: wongpratan Re: Switch Case?? - 06/08/06 12:43 PM
thank a lot
Posted By: wongpratan Re: Switch Case?? - 06/08/06 12:53 PM
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
confused
Posted By: RusselB Re: Switch Case?? - 06/08/06 05:22 PM
From Hixxy's post:
Quote:
if it followed C
Posted By: wongpratan Re: Switch Case?? - 06/08/06 05:56 PM
ohhh i see blush
Posted By: RusselB Re: Switch Case?? - 06/08/06 06:54 PM
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.
Posted By: hixxy Re: Switch Case?? - 06/08/06 07:00 PM
I explained it above. Read the comments.
Posted By: genius_at_work Re: Switch Case?? - 06/08/06 07:34 PM
https://forums.mirc.com/showflat.php?Number=157834#Post157834

-genius_at_work
Posted By: Mardeg Re: Switch Case?? - 11/08/06 10:15 AM
Here's a pseudo switch thingy for giving time on join. Note: it's untested and I probably wouldn't suggest coding this way:
Code:
 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
Posted By: FNar Re: Switch Case?? - 14/08/06 01:52 AM
Quote:
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.
Posted By: NaquadaServ Re: Switch Case?? - 14/08/06 03:21 PM
Quote:
Quote:
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)". smile

You could also script this to some point... wink
Code:
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 }
  }
}
Posted By: starbucks_mafia Re: Switch Case?? - 14/08/06 05:27 PM
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.
Posted By: DaveC Re: Switch Case?? - 15/08/06 12:00 PM
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?
Posted By: starbucks_mafia Re: Switch Case?? - 15/08/06 07:16 PM
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.
Posted By: DaveC Re: Switch Case?? - 15/08/06 10:24 PM
Still its the same thing discussed maybe 6 months ago now isnt it.
Posted By: starbucks_mafia Re: Switch Case?? - 16/08/06 12:29 PM
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.
Posted By: Yil Re: Switch Case?? - 17/08/06 01:42 AM
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...
Posted By: x64 Re: Switch Case?? - 20/08/06 10:31 AM
A switch/case would be a great enhancement to speed and efficiency of not only runtime but of coding too. Consider the following script:

if (%command == ban) { ...
} elseif (%command == unban) { ...
} elseif (%command == op) { ...
} elseif (%command == deop) { ...
} elseif (%command == voice) { ...
} elseif (%command == devoice) { ...
}

%command must be evaluated 6 times here in an unbalanced situation (the first commands will execute much sooner than commands at the end of the clause).

In a switch/case:

switch (%command) {
case (unban) ...
case (ban) ...
case (op) ...
case (deop) ...
case (voice) ...
case (devoice) ...
}

Here, %command is evaluated once, the code is 10 times clearer, and if all cases are literal strings, mirc could internally use a hashtable which would mean that all commands would take the same amount of time to execute regardless of where they are on the sequence.
Posted By: Om3n Re: Switch Case?? - 20/08/06 11:34 AM
this would be good indeed,, however it feels a little like 'foreach' dejavu
Posted By: IAmParadox Re: Switch Case?? - 01/09/06 11:32 PM
run this through your alias and see how it does:
http://www.nomorepasting.com/paste.php?pasteID=67981


EDIT: Fixed a little error
Posted By: IAmParadox Re: Switch Case?? - 02/09/06 03:03 AM
I couldn't edit my last post anymore, so here's a version of it without any gotos:

http://www.nomorepasting.com/paste.php?pasteID=67985
Posted By: IAmParadox Re: Switch Case?? - 03/09/06 03:50 AM
I guess no one is even reading this thread anymore, but I thought I would add if you put the whole switch case in a while, the break works more like you would expect it to.
© mIRC Discussion Forums