mIRC Homepage
Posted By: The_JD Select case - 21/07/08 10:38 AM
is there anything like "select case"/"switch"?

eg:

Code:
case $1 {
 :001 return we got 001
 :002 echo -a Something else
 :default echo -a Default case
}


at the moment im using something like that, but with Goto...

eg
Code:
goto $1
 :001
  { return we got 001 | goto break }
 :002
  { echo -a Something else | goto break }
 :break
 


(no default)

Is there a cleaner way to do this?
Posted By: WhipLash Re: Select case - 21/07/08 11:23 AM
Im gonna ask the really dumb question:

What case are we talking about? Cause 001 is ascii for some special character, and 002 is the ascii for bold.

As for case:
Quote:
$islower(text)
Returns $true if text is all lower case.

$isupper(text)
Returns $true if text is all upper case.



But somehow i think mean the control codes..
Posted By: The_JD Re: Select case - 21/07/08 11:35 AM
Lol Sorry I mean a function that is similar to PHP's switch function...

See http://au2.php.net/switch
Posted By: WhipLash Re: Select case - 21/07/08 12:02 PM
Ah....
While i prsonally cant answer this, but i'd also like to know.
Posted By: genius_at_work Re: Select case - 21/07/08 02:17 PM
There is presently no CASE structure in mIRC, though it has been requested many times.

I've seen it simulated with goto and labels.

-genius_at_work
Posted By: The_JD Re: Select case - 21/07/08 03:56 PM
So I guess the way I'm doing it is the way to go then
Posted By: argv0 Re: Select case - 21/07/08 05:14 PM
why not just do

Code:
if ($1 == 001) { echo -a we got 001 }
elseif ($1 == 002) { echo -a we got 002 }


Anyone who says "but that's so much more code!" probably also believes in Santa Claus. It only starts becoming "more" after 10 cases at least, and anything more than 10 cases shouldn't even be a case switch anymore in mIRC-- there are many better ways to code a decision list for large decision item lists.

Posted By: hixxy Re: Select case - 21/07/08 08:15 PM
You can use default cases with your method like this:

Code:
var %a = $1
goto %a
 :001
  { return we got 001 | goto break }
 :002
  { echo -a Something else | goto break }
 :%a
  { default case code here }
 :break
Posted By: The_JD Re: Select case - 22/07/08 01:48 PM
Originally Posted By: argv0
why not just do

Code:
if ($1 == 001) { echo -a we got 001 }
elseif ($1 == 002) { echo -a we got 002 }


Anyone who says "but that's so much more code!" probably also believes in Santa Claus. It only starts becoming "more" after 10 cases at least, and anything more than 10 cases shouldn't even be a case switch anymore in mIRC-- there are many better ways to code a decision list for large decision item lists.



I'm using it to display all error messages in a large script

Originally Posted By: hixxy
You can use default cases with your method like this:

Code:
var %a = $1
goto %a
 :001
  { return we got 001 | goto break }
 :002
  { echo -a Something else | goto break }
 :%a
  { default case code here }
 :break


Very nice idea... Dont know why I didnt think of that... Imma use it :P
Posted By: MeStinkBAD Re: Select case - 31/07/08 09:17 AM
Originally Posted By: argv0
why not just do

Code:
if ($1 == 001) { echo -a we got 001 }
elseif ($1 == 002) { echo -a we got 002 }


Anyone who says "but that's so much more code!" probably also believes in Santa Claus. It only starts becoming "more" after 10 cases at least, and anything more than 10 cases shouldn't even be a case switch anymore in mIRC-- there are many better ways to code a decision list for large decision item lists.


The amount of code is irrelevant. It's about clean looking code that can be easily understood by a quick glance. Cleaner code makes it easier to spot mistakes, resolve bugs, and just have a better idea of what the code is designed to do. It also makes code simpler to edit or extend.


Posted By: Wims Re: Select case - 31/07/08 05:21 PM
Originally Posted By: MeStinkBAD
The amount of code is irrelevant. It's about clean looking code that can be easily understood by a quick glance. Cleaner code makes it easier to spot mistakes, resolve bugs, and just have a better idea of what the code is designed to do. It also makes code simpler to edit or extend.
This is true, and that's why i agree with argv0, use if/esleif instead of your horrible thing is really much cleaner, more easy to read/edit/extend and i would have a better idea about what the code do with if/elseif rather than with something else.
Posted By: starbucks_mafia Re: Select case - 31/07/08 06:01 PM
if/elseif is not the same as a select statement. Each case of a select statement cascades into the next unless explicitly told not to - this behaviour scales very poorly using if's both in terms of code readability and efficiency.

Goto is the only reasonable alternative currently, but that has a number of serious limitations too.
Posted By: argv0 Re: Select case - 31/07/08 06:39 PM
People rarely intentionally make use of switch cascading, though it's the only benefit. I'll bet the OP of this thread wasn't planning on it...

So the real benefit you get out of one use case is marginal, and if statements really aren't any more unreadable than goto switches when used normally. Like I said before, you don't want to have 10+ consecutive if statements in ONE alias/event just like you don't want to have 10+ consecutive switch cases in one alias/event.. they're both going to be equally large and unmanageable chunks of code. It's not about syntax, it's about size.
Posted By: Mpdreamz Re: Select case - 31/07/08 09:22 PM
Then again using cascading to your benefit could possibly be a very efficient and descriptive way of writing a routine.

It's funny that in languages that do have switch/select statements alot of whisper hits the scene to get rid of elseif'ing as coding practise. Funny sidenote: VB's Select case doesnt even cascade.

For that reason if this ever were to be implemented we should stick to switch/case/default as keywords and starbucks_maffia suggestion:
Quote:


In addition to the switch/case/default suggestion here I'll tag on a suggestion for an "else" clause in place of "default" which only triggers if no other cases have triggered (basically a "default" which can't be cascaded into).

definitely has my vote.
© mIRC Discussion Forums