mIRC Home    About    Download    Register    News    Help

Print Thread
Page 1 of 2 1 2
PHP/C# Style 'SWITCH' statements #162162
15/10/06 02:33 AM
15/10/06 02:33 AM
Joined: Oct 2006
Posts: 48
G
Ghozer Offline OP
Ameglian cow
Ghozer  Offline OP
Ameglian cow
G

Joined: Oct 2006
Posts: 48
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..

Last edited by Ghozer; 15/10/06 02:46 AM.
Re: PHP/C# Style 'SWITCH' statements #162163
15/10/06 03:28 AM
15/10/06 03:28 AM
Joined: Aug 2004
Posts: 7,252
Ontario, Canada
R
RusselB Offline
Hoopy frood
RusselB  Offline
Hoopy frood
R

Joined: Aug 2004
Posts: 7,252
Ontario, Canada
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.

Re: PHP/C# Style 'SWITCH' statements #162164
15/10/06 03:37 AM
15/10/06 03:37 AM
Joined: Oct 2006
Posts: 48
G
Ghozer Offline OP
Ameglian cow
Ghozer  Offline OP
Ameglian cow
G

Joined: Oct 2006
Posts: 48
The example was exactly that... an example....

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

Re: PHP/C# Style 'SWITCH' statements #162165
15/10/06 05:52 AM
15/10/06 05:52 AM
Joined: Sep 2003
Posts: 4,230
D
DaveC Offline
Hoopy frood
DaveC  Offline
Hoopy frood
D

Joined: Sep 2003
Posts: 4,230
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.

Re: PHP/C# Style 'SWITCH' statements #162166
15/10/06 03:21 PM
15/10/06 03:21 PM
Joined: Oct 2006
Posts: 48
G
Ghozer Offline OP
Ameglian cow
Ghozer  Offline OP
Ameglian cow
G

Joined: Oct 2006
Posts: 48
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 () {} -- )

Re: PHP/C# Style 'SWITCH' statements #162167
15/10/06 04:34 PM
15/10/06 04:34 PM
Joined: Oct 2004
Posts: 8,327
MA, USA
Riamus2 Offline
Hoopy frood
Riamus2  Offline
Hoopy frood

Joined: Oct 2004
Posts: 8,327
MA, USA
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


Invision Support
#Invision on irc.irchighway.net
Re: PHP/C# Style 'SWITCH' statements #162168
16/10/06 02:05 AM
16/10/06 02:05 AM
Joined: Oct 2006
Posts: 48
G
Ghozer Offline OP
Ameglian cow
Ghozer  Offline OP
Ameglian cow
G

Joined: Oct 2006
Posts: 48
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

Re: PHP/C# Style 'SWITCH' statements #162169
16/10/06 02:16 AM
16/10/06 02:16 AM
Joined: Aug 2004
Posts: 7,252
Ontario, Canada
R
RusselB Offline
Hoopy frood
RusselB  Offline
Hoopy frood
R

Joined: Aug 2004
Posts: 7,252
Ontario, Canada
For PHP, that's true, however, Riamus' statement was in regards to C++ not PHP

Re: PHP/C# Style 'SWITCH' statements #162170
16/10/06 02:19 AM
16/10/06 02:19 AM
Joined: Oct 2004
Posts: 8,327
MA, USA
Riamus2 Offline
Hoopy frood
Riamus2  Offline
Hoopy frood

Joined: Oct 2004
Posts: 8,327
MA, USA
Right. smile

I actually forgot that this also mentioned PHP.


Invision Support
#Invision on irc.irchighway.net
Re: PHP/C# Style 'SWITCH' statements #162171
16/10/06 03:53 AM
16/10/06 03:53 AM
Joined: Sep 2003
Posts: 4,230
D
DaveC Offline
Hoopy frood
DaveC  Offline
Hoopy frood
D

Joined: Sep 2003
Posts: 4,230
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

Re: PHP/C# Style 'SWITCH' statements #162172
16/10/06 07:45 AM
16/10/06 07:45 AM
Joined: May 2006
Posts: 122
J
jizzy Offline
Vogon poet
jizzy  Offline
Vogon poet
J

Joined: May 2006
Posts: 122
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 
} 


If only women came with popup menus and online help.
Re: PHP/C# Style 'SWITCH' statements #162173
16/10/06 08:33 AM
16/10/06 08:33 AM
Joined: Oct 2004
Posts: 73
M
Mardeg Offline
Babel fish
Mardeg  Offline
Babel fish
M

Joined: Oct 2004
Posts: 73
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...

Re: PHP/C# Style 'SWITCH' statements #162174
16/10/06 10:54 AM
16/10/06 10:54 AM
Joined: Apr 2004
Posts: 759
Arnhem, The Netherlands
M
Mpdreamz Offline
Hoopy frood
Mpdreamz  Offline
Hoopy frood
M

Joined: Apr 2004
Posts: 759
Arnhem, The Netherlands
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.


$maybe
Re: PHP/C# Style 'SWITCH' statements #162175
16/10/06 01:21 PM
16/10/06 01:21 PM
Joined: Jan 2003
Posts: 2,523
Q
qwerty Offline
Hoopy frood
qwerty  Offline
Hoopy frood
Q

Joined: Jan 2003
Posts: 2,523
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.


/.timerQ 1 0 echo /.timerQ 1 0 $timer(Q).com
Re: PHP/C# Style 'SWITCH' statements #162176
16/10/06 02:09 PM
16/10/06 02:09 PM
Joined: Oct 2006
Posts: 48
G
Ghozer Offline OP
Ameglian cow
Ghozer  Offline OP
Ameglian cow
G

Joined: Oct 2006
Posts: 48

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

Last edited by Ghozer; 16/10/06 02:11 PM.
Re: PHP/C# Style 'SWITCH' statements #162177
16/10/06 02:42 PM
16/10/06 02:42 PM
Joined: Dec 2002
Posts: 2,962
Norwich, UK
S
starbucks_mafia Offline
Hoopy frood
starbucks_mafia  Offline
Hoopy frood
S

Joined: Dec 2002
Posts: 2,962
Norwich, UK
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).


Spelling mistakes, grammatical errors, and stupid comments are intentional.
Re: PHP/C# Style 'SWITCH' statements #162178
16/10/06 03:22 PM
16/10/06 03:22 PM
Joined: Oct 2006
Posts: 48
G
Ghozer Offline OP
Ameglian cow
Ghozer  Offline OP
Ameglian cow
G

Joined: Oct 2006
Posts: 48
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

Re: PHP/C# Style 'SWITCH' statements #162179
16/10/06 03:42 PM
16/10/06 03:42 PM
Joined: Jan 2003
Posts: 2,523
Q
qwerty Offline
Hoopy frood
qwerty  Offline
Hoopy frood
Q

Joined: Jan 2003
Posts: 2,523
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).


/.timerQ 1 0 echo /.timerQ 1 0 $timer(Q).com
Re: PHP/C# Style 'SWITCH' statements #162180
16/10/06 03:46 PM
16/10/06 03:46 PM
Joined: Oct 2006
Posts: 48
G
Ghozer Offline OP
Ameglian cow
Ghozer  Offline OP
Ameglian cow
G

Joined: Oct 2006
Posts: 48
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..

Re: PHP/C# Style 'SWITCH' statements #162181
16/10/06 04:00 PM
16/10/06 04:00 PM
Joined: Jan 2003
Posts: 2,523
Q
qwerty Offline
Hoopy frood
qwerty  Offline
Hoopy frood
Q

Joined: Jan 2003
Posts: 2,523
What are you talking about? There is no /break or /halt command in the code.


/.timerQ 1 0 echo /.timerQ 1 0 $timer(Q).com
Page 1 of 2 1 2