mIRC Home    About    Download    Register    News    Help

Print Thread
Alternative for using goto for 'on case' situation #222871 07/07/10 07:53 AM
Joined: Dec 2002
Posts: 122
S
STING Offline OP
Vogon poet
OP Offline
Vogon poet
S
Joined: Dec 2002
Posts: 122
I feel a bit like a noob for asking, but is there a better altnerative for using structures like:

Code:

on *:text:#: {
if ($1) { goto $1 } | else { halt }
:1 | echo -a choice #1 | halt
:2 | echo -a choice #2 | halt
:3 | echo -a choice #3 | halt
}

or

on *:text:#: {
if ($1 == 1) { echo -a choice #1 | halt }
if ($1 == 2) { echo -a choice #1 | halt }
if ($1 == 3) { echo -a choice #1 | halt }
}


Re: Alternative for using goto for 'on case' situation [Re: STING] #222874 07/07/10 08:01 AM
Joined: Aug 2004
Posts: 7,252
R
RusselB Offline
Hoopy frood
Offline
Hoopy frood
R
Joined: Aug 2004
Posts: 7,252
Aside from correcting the ON TEXT events so that they use the correct format, the 2nd structure could use if/elseif/else rather than several if's with halt's.

I'm going to presume that the scenario you gave is a generalization, and not specific to your actual needs.

Re: Alternative for using goto for 'on case' situation [Re: RusselB] #222875 07/07/10 08:14 AM
Joined: Dec 2002
Posts: 122
S
STING Offline OP
Vogon poet
OP Offline
Vogon poet
S
Joined: Dec 2002
Posts: 122
Originally Posted By: RusselB
Aside from correcting the ON TEXT events so that they use the correct format, the 2nd structure could use if/elseif/else rather than several if's with halt's.


alias blah {
if ($1 == 1) { blah }
if ($1 == 2) { blah }
if ($1 == 3) { blah }
}

alias blah {
if ($1 == 1) { blah }
elseif ($1 == 2) { blah }
elseif ($1 == 3) { blah }
}

I use elseif mainly for stacking if-statements, does is have any added value in this scenario?

Quote:

I'm going to presume that the scenario you gave is a generalization, and not specific to your actual needs.


Correct.

Re: Alternative for using goto for 'on case' situation [Re: STING] #222876 07/07/10 08:22 AM
Joined: Feb 2006
Posts: 546
J
jaytea Offline
Fjord artisan
Offline
Fjord artisan
J
Joined: Feb 2006
Posts: 546
heya STING! /goto is usually the alternative proposed, the usual method being a string of if statements. though it doesn't serve as a general purpose example without some further consideration, there are are other ways to take advantage of some of /goto's features:

Code:
  if ($istok(1 2 3, $1, 32)) goto $1
  if ($1 != $null) {
    echo -a default!
    halt
  }
  echo -a null!
  halt  
  :1
  echo -a 1!
  halt
  :2
  echo -a 2!
  halt
  :3
  echo -a 3!
  halt


the first line, the $istok() check, is just to ensure that $1 exists in your set of selections, otherwise you'd get an error saying the label you want to goto doesn't exist. now, you can perform a little trickery by coming up with a label corresponding to a value of $1 that you never expect to have and use the following:

Code:
  var %goto = $1
  goto $1 .
  :1
  echo -a 1!
  halt
  :2
  echo -a 2!
  halt
  :3
  echo -a 3!
  halt
  :.
  echo -a null!
  halt
  :%goto
  echo -a default!
  halt


if $1 is $null then /goto %goto . becomes /goto .

if it isn't $null but isn't one of the valid selections, %goto serves as the default case

this isn't really an alternative as you requested, just something else to think about when using /goto


"The only excuse for making a useless script is that one admires it intensely" - Oscar Wilde
Re: Alternative for using goto for 'on case' situation [Re: jaytea] #222877 07/07/10 08:26 AM
Joined: Dec 2002
Posts: 122
S
STING Offline OP
Vogon poet
OP Offline
Vogon poet
S
Joined: Dec 2002
Posts: 122
Originally Posted By: jaytea

this isn't really an alternative as you requested, just something else to think about when using /goto


Thanks, the $istok & goto %goto are good tips.
I think I'll stick with the goto's. It's only for dialog event handling, so no real large need for speed efficiency there wink


Re: Alternative for using goto for 'on case' situation [Re: STING] #222879 07/07/10 08:29 AM
Joined: Feb 2006
Posts: 546
J
jaytea Offline
Fjord artisan
Offline
Fjord artisan
J
Joined: Feb 2006
Posts: 546
Originally Posted By: STING
Originally Posted By: RusselB
Aside from correcting the ON TEXT events so that they use the correct format, the 2nd structure could use if/elseif/else rather than several if's with halt's.


alias blah {
if ($1 == 1) { blah }
if ($1 == 2) { blah }
if ($1 == 3) { blah }
}

alias blah {
if ($1 == 1) { blah }
elseif ($1 == 2) { blah }
elseif ($1 == 3) { blah }
}

I use elseif mainly for stacking if-statements, does is have any added value in this scenario?


it only serves to prevent mIRC from going through subsequent if conditions should one of them succeed which, if you're using /halt in each of them, doesn't happen anyway. if you aren't, the elseif approach provides a comfortable way of including a default case (else {})


"The only excuse for making a useless script is that one admires it intensely" - Oscar Wilde
Re: Alternative for using goto for 'on case' situation [Re: jaytea] #222880 07/07/10 12:05 PM
Joined: Oct 2004
Posts: 8,330
Riamus2 Offline
Hoopy frood
Offline
Hoopy frood
Joined: Oct 2004
Posts: 8,330
You're also better off using RETURN rather than HALT in most situations, though ELSEIF is a better option, imo.

There are a variety of ways you can avoid the need for GOTO and even avoid many ELSEIF lines. Sometimes, just using variables creatively can save you a lot of lines of code. Or, for only a couple possible inputs, $iif() might be a good option. It all depends on exactly what you're trying to do. A real example is more likely to get you good advice than a fake example.


Invision Support
#Invision on irc.irchighway.net
Re: Alternative for using goto for 'on case' situation [Re: RusselB] #222883 07/07/10 01:12 PM
Joined: Feb 2009
Posts: 133
C
chacha Offline
Vogon poet
Offline
Vogon poet
C
Joined: Feb 2009
Posts: 133
in this case i think that /goto avoids several conditions then
it avoids many unnecessary calculations


WorldDMT