mIRC Homepage

PHP/C# Style 'SWITCH' statements

Posted By: Ghozer

PHP/C# Style 'SWITCH' statements - 15/10/06 02:33 AM

Please PLEASE add SWITCH's for Scripting within mIRC, to replace lots of Ifs... an example would be..

With IF's....
alias moo {
var %moo = $1
if (%moo == cow) { echo -a moo cow }
else if (%moo == dog) { echo -a moo dog }
else if (%moo == cat) { echo -a moo cat }
else { echo -a moo mouse }
}

With Switch's....

alias moo {
var %moo = $1
switch(moo) {
case "cow":
echo -a moo cow
break
case "dog":
echo -a moo dog
break
case "cat":
echo -a moo cat
break
default:
echo -a moo mouse
break
}
}

this could be done using a variable, or direct switch($1) { } etc...

break is used to break out of the switch, so it doesnt check any further (as it doesnt need to, and it wont have any more).. however should NOT break out of the alias..
Posted By: RusselB

Re: PHP/C# Style 'SWITCH' statements - 15/10/06 03:28 AM

Already suggested here

Additonally your example isn't the greatest
Code:
 alias moo {
echo -a moo $iif($istok(cow dog cat,$1,32),$1,mouse)
}
  


Now, I can see an arguement in that the above is set for only four words, but your example uses those exact same four words.
Posted By: Ghozer

Re: PHP/C# Style 'SWITCH' statements - 15/10/06 03:37 AM

The example was exactly that... an example....

i wasnt going to sit here, and type out a long and complex script etc...
Posted By: DaveC

Re: PHP/C# Style 'SWITCH' statements - 15/10/06 05:52 AM

Quote:
Please PLEASE add SWITCH's for Scripting within mIRC, to replace lots of Ifs... an example would be..

yes ift does replace lots of if's but what besides IF's would it really be? the case value would be stored, and then compared to each case select, whats that besides a bunch of ifs.

Actually a switch case doesnt work the way you showed using IF's, sicne if you dont BREAK in a case then the following code in the next case is also executed, untill it ends or

however with little work it can be preformed using if's, if have added a "d&c" case which is ment to show duck and cat case code.
Code:
alias moo {
  var %moo = $1, %in = $false
  while (!%in)
    if (%in) || (%moo == cow) { %in = $true | echo -a moo cow | break }
    if (%in) || (%moo == dog) { %in = $true | echo -a moo dog | break }
    if (%in) || (%moo == d&c) { %in = $true | echo -a moo duck }
    if (%in) || (%moo == cat) { %in = $true | echo -a moo cat | break }
    echo -a noo mouse | break
    %in = $true 
  }
}


I have a couple of times said we dont need a case select statement, however im not against it in princible if it was added, just that it isnt really needed
It only is going to simplify a not overly complexe system as shown above. And i beleiev would be quite hard to code, considering the scripts are interpreted rather than compiled.

PS: the while loop is in theer to make use of the mirc /break command, it is never ment to be used to execute multiple passes of the code, as seen by the final command %in = $true, even tho that is never reached.
Posted By: Ghozer

Re: PHP/C# Style 'SWITCH' statements - 15/10/06 03:21 PM

Im thinking from a look point of view,

the examples on this thread you cant just Glance over and see 'thats right' a switch with case statements allows this, and it looks alot tidier, Your example Looks alot more complicated, and cant be glanced over, in an instant...

(also.. you dont have to have a break btw, I was doing that to make it the same as the if/else if, no break would be the same as if () {} | if () {} -- )
Posted By: Riamus2

Re: PHP/C# Style 'SWITCH' statements - 15/10/06 04:34 PM

Personally, I can glance over the normal IF/ELSEIF/ELSE scripts quickly and easily, but I've been using them for 5 years. It has been 15+ since I've messed with C++, so it's much easier for me to quickly glance at the current setup than to look over your switches.

I'm not saying it shouldn't be added, but keep in mind that this isn't C++. It's mIRC. It isn't programming. It's scripting. It isn't compiled. It's run.

laugh
Posted By: Ghozer

Re: PHP/C# Style 'SWITCH' statements - 16/10/06 02:05 AM

the "isnt compiled, its run" statement is false, because PHP is scripting, isnt compiled, and is just simply "run"

so that argument holds no ground smile
Posted By: RusselB

Re: PHP/C# Style 'SWITCH' statements - 16/10/06 02:16 AM

For PHP, that's true, however, Riamus' statement was in regards to C++ not PHP
Posted By: Riamus2

Re: PHP/C# Style 'SWITCH' statements - 16/10/06 02:19 AM

Right. smile

I actually forgot that this also mentioned PHP.
Posted By: DaveC

Re: PHP/C# Style 'SWITCH' statements - 16/10/06 03:53 AM

Quote:
(also.. you dont have to have a break btw, I was doing that to make it the same as the if/else if, no break would be the same as if () {} | if () {} -- )


Amazing you told me that, considering i pointed it out to you. smile
Posted By: jizzy

Re: PHP/C# Style 'SWITCH' statements - 16/10/06 07:45 AM

A similar method is already possible:
Code:
alias moo { 
var %moo = $1 
goto %moo
:cow
echo -a moo cow 
return
:dog 
echo -a moo dog 
return
:cat 
echo -a moo cat 
return
:%moo
echo -a moo mouse 
return 
} 
Posted By: Mardeg

Re: PHP/C# Style 'SWITCH' statements - 16/10/06 08:33 AM

Has anyone else noticed that although the use of goto isn't looked upon well because it makes for hard-to-read or "spaghetti" code, the most common topic it comes up in is as a switch-like workaround designed to make the code easier-to-read at a glance...
Posted By: Mpdreamz

Re: PHP/C# Style 'SWITCH' statements - 16/10/06 10:54 AM

Its offered as an alternative however i dont think its easy to read, as while looking over the code and you come accros a :goto theres nothing that tells you its used as a switch (talking larger pieces of code) though goto %var or :%var is usually a good indication. It also doesn't indent nicely as a proper case/switch statement would.
Posted By: qwerty

Re: PHP/C# Style 'SWITCH' statements - 16/10/06 01:21 PM

This works fine wink
Code:
alias moo { 
  var %moo = $1 
  goto %moo {
    :cow 
    echo -a moo cow 
    return 
 
    :dog 
    echo -a moo dog 
    return 
 
    :cat 
    echo -a moo cat 
    return 
 
    :%moo 
    echo -a moo mouse 
    return
  }
}
I know some will scream "abuse" but I find this more readable than a bunch of ifs. In fact, the only difference between the above and the suggested switch/case is the usage of the "goto" keyword, instead of "switch", and the absense of the "case" keyword.
Posted By: Ghozer

Re: PHP/C# Style 'SWITCH' statements - 16/10/06 02:09 PM


Quote:
For PHP, that's true, however, Riamus' statement was in regards to C++ not PHP


and I never mentioned C++, as you cant use Strings in switches in C++, only in C# or PHP.. wink so still.. although C# is compiled, the fact still remains that references to C++ are pointless ;P
Posted By: starbucks_mafia

Re: PHP/C# Style 'SWITCH' statements - 16/10/06 02:42 PM

That's a nice use of brackets there. I must admit I'd never even thought of that to make goto more readable.

Although that code does emulate the majority of functions a scripter would use a switch statement for there are a few things where it falls short...
  • There is no way to break from the switch without leaving the entire scope (unless you use an additional variable check per case of course).
  • If you have multiple switches in the same peice of code they mustn't have duplicate case values
  • Limited values for the case value (ie. only literals or variables, no identifiers, no spaces)
  • You can't have a case with the value "error" alongside error handling code (OK now I'm just picking holes).


Only the first one of those is really a problem, but it is a pretty significant one. Trying to emulate break effectively renders the code unreadable and you lose that nice warm feeling that your code is efficient.

The goto method is an elegant hack, but it is a hack nonetheless. I'm sure no-one is ever going to die from the lack of a switch statement in mIRC but it sure wouldn't suck if it ever popped up.

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).
Posted By: Ghozer

Re: PHP/C# Style 'SWITCH' statements - 16/10/06 03:22 PM

the default case would be optional..

yes, default would trigger if no other case statements were used or accessed, You could also use it as an 'ending' or 'closing' statment too (if you omit a break from a case statement (or all), it will continue to check, and hit default, and execute it)

There are many reasons for adding a switch/case statement and compatability in mIRC, there's also many reasons not to, but i think there's more reasons to add it, and many people would love it.. smile
Posted By: qwerty

Re: PHP/C# Style 'SWITCH' statements - 16/10/06 03:42 PM

True, there are some functional limitations. My comment was more about looks and readability (assuming sufficient functionality) and is not to be taken as an argument against switch/case support (I've never wished for it, let alone needed it, but I have nothing against it either).

An idea (and just that) regarding the first limitation could be this:
Code:
alias moo { 
  var %moo = $1 
  goto %moo {
    :cow 
    echo -a moo cow 
    goto break 
 
    :dog 
    echo -a moo dog 
    goto break
 
    :cat 
    echo -a moo cat 
    goto break
 
    :%moo
    echo -a moo mouse 
  } | :break
}

Granted, this uglifies the code a little bit, and the same "goto break" command can't be used in more than one blocks etc, but it's not inefficient in any (significant) way.

The inability to have labels with spaces or identifiers is more serious imo though (and perhaps the only use I'd have for switch in my scripts).
Posted By: Ghozer

Re: PHP/C# Style 'SWITCH' statements - 16/10/06 03:46 PM

and what about when you wish to continue the script AFTER the 'switch' (goto's) the break or a halt would prevent further script execution..
Posted By: qwerty

Re: PHP/C# Style 'SWITCH' statements - 16/10/06 04:00 PM

What are you talking about? There is no /break or /halt command in the code.
Posted By: Jigsy

Re: PHP/C# Style 'SWITCH' statements - 16/10/06 04:16 PM

He ment goto break.
Posted By: Ghozer

Re: PHP/C# Style 'SWITCH' statements - 16/10/06 05:06 PM

Quote:
What are you talking about? There is no /break or /halt command in the code.


yeah, i was meaning the goto break, i was speaking literal re: yours and previous 'hack' versions and ways round it..
Posted By: DaveC

Re: PHP/C# Style 'SWITCH' statements - 16/10/06 11:10 PM

Better would be this

Code:
alias moo { 
  var %moo = $1
  var %in = $true | while (%in) { var %in = $false
    goto %moo {
      :cow 
      echo -a moo cow 
      break 
 
      :dog 
      echo -a moo dog 
      break
 
      :d&c
      echo -a moo duck
 
       :cat 
      echo -a moo cat 
      break
 
      :%moo 
      echo -a moo mouse 
      break
    }
  }
  echo -a code beyond the case
}


thats pretty read able as well, and we even get to use the real /BREAK command

[edit]
I just thought as well it leads to another interesting option, the ability to alter %moo and recall the case select if you so desired, by use of the /CONTINUE comand, forcing it to return to the top of the while loop.
Posted By: Mpdreamz

Re: PHP/C# Style 'SWITCH' statements - 16/10/06 11:41 PM

Hey cool nicely "abused" mate never thought of that before. Its still not indented correctly but still the half of something is better then a whole lot of nothing smile

if implemented i would also like to see the case statement using () as well as suppose to JS/PHP since we dont use quoted strings
Code:
switch (%var) { 
  case (dog) { 
    echo -a what you know! its a dog!
    break
  }
  default { 
    echo -a what you know! its not a dog :(
  }
}

Though default doesn't really need bracketing there :tongue:
Posted By: qwerty

Re: PHP/C# Style 'SWITCH' statements - 16/10/06 11:55 PM

It would be much more readable without the extra pair of braces and the additional indentation of the while block, but it's an interesting idea nonetheless.
© 2019 mIRC Discussion Forums