mIRC Home    About    Download    Register    News    Help

Print Thread
#150134 28/05/06 06:33 PM
Joined: May 2006
Posts: 1
T
Timmm Offline OP
Mostly harmless
OP Offline
Mostly harmless
T
Joined: May 2006
Posts: 1
I scanned the posts and didn't see anything about this - wondering if it would be possible to include a simple switch/case function in the script host - I'm building an eggdrop-style partyline and have probably a couple dozen commands I want to implement.. using if/elseif seems messy to me.. I'd love to be able to do something like:

console_command_parse {
switch $1 {

case ".jump" {
;jump code
}

case ".whom" {
;whom code
}
}
}

Tim

#150135 28/05/06 07:22 PM
Joined: Jan 2003
Posts: 3,012
Hoopy frood
Offline
Hoopy frood
Joined: Jan 2003
Posts: 3,012
While searching, be sure to expand the date range to read:

Code:
Newer than [1___][_______[v] (Deselect the week option)
Older than [____][_______[v]


Additionally, select all forum heading on the left. You may try this topic if you'd like an idea for a work-around.


-KingTomato
#150136 28/05/06 09:15 PM
Joined: Dec 2002
Posts: 252
T
Fjord artisan
Offline
Fjord artisan
T
Joined: Dec 2002
Posts: 252
make an alias for each individual command. have that alias handle what the command should do. I do this for my IRCd I wrote.. then you only need one if statement.

alias UserCMDS {
var %handle = $+(handle_,$1)
if ($isalias(%handle)) { %handle $2- }
else { SendNumericR $IRCDS 421 $1 :Unknown command }
}

alias handle_ping {
if ($1) { senddata : $+ $IRCDS PONG $ircds $1- }
else { SendNumeric $IRCDS 409 No origin specified. }
}

#150137 29/05/06 01:03 AM
Joined: Sep 2003
Posts: 4,230
D
Hoopy frood
Offline
Hoopy frood
D
Joined: Sep 2003
Posts: 4,230
i see little difference between that and

Code:
console_command_parse {
  if ($1 == .jump) {
    ;jump code
  }
  elseif ($1 == .whom) {
    ;whom code
  }
  elseif ($1 == .bob) {
    ;bob code
  }
  elseif ($1 == .fred) {
    ;fred code
  }
}


There maight be some fractional speed difference, but considering its a keyboard command your talking about, i hardly expect thsi to be in use so drasticly that it effects you.

#150138 29/05/06 01:44 AM
Joined: Oct 2005
Posts: 1,741
G
Hoopy frood
Offline
Hoopy frood
G
Joined: Oct 2005
Posts: 1,741
I think the main purpose for switch structures (in other languages) is to allow "fall-through" situations. Example:

Code:
switch %x {
  case 1:
  case 2:
    echo -a x = 1 or 2
    break
  case 3:
  case 4:
  case 9:
    echo -a x = 3 or 5 or 9
    break
  default:
    echo -a x is invalid
    break
}


@OP

You may be able to make a pseudo switch/case structure using labels and /goto commands.

Code:
alias switch {
  echo -a - - - START - - -
  var %sw = $1
  goto %sw
  :0
  :1
  :2
  echo -a 0 or 1 or 2
  goto end
  :3
  :4
  echo -a 3 or 4
  goto end
  :5
  echo -a 5
  goto end

  ;;; 'default' switch
  :error
  echo -a Invalid: %sw
  reseterror
  goto end

  :end
  echo -a - - - END - - -
}


-genius_at_work

#150139 29/05/06 01:15 PM
Joined: Sep 2003
Posts: 4,230
D
Hoopy frood
Offline
Hoopy frood
D
Joined: Sep 2003
Posts: 4,230
Quote:
I think the main purpose for switch structures (in other languages) is to allow "fall-through" situations. Example:


This has all been hashed over at least once before. Sure its usefull, but its not something that cant be emulated by the simple IF statement.
I think khaled just doesnt see any reason to have to make it more complex on the scripting phraser than it already is

Dont get me wrong, i would be one of the first to use it, if it had it, but im not sobbing into my cornflakes that its not here either.

as for drop through

Code:
while ($1 == $1) {
  var %dropthrough = $false
  if (($1 == .fred) || %dropthrough) { %dropthrough = $true | ..... .fred code ..... | break }
  if (($1 == .mike) || %dropthrough) { %dropthrough = $true | ..... .mike code ..... }
  if (($1 == .bill) || %dropthrough) { %dropthrough = $true | ..... .bill code ..... | break }  
  .... else code ....
  break
}


yours even, using smaller vars so as to not over welm whats important...

Code:
while (%x == %x) {
  var %dt = $false
  if (%dt || (%x == 1)) { %dt = $true }
  if (%dt || (%x == 2)) { %dt = $true
    echo -a x = 1 or 2
    break }
  if (%dt || (%x == 3)) { %dt = $true }
  if (%dt || (%x == 5)) { %dt = $true }
  if (%dt || (%x == 5)) { %dt = $true 
    echo -a x = 3 or 5 or 9
    break }
  ;default:
  echo -a x is invalid
  break
}

* moved the %dt to the front to speed up comparasions on already matched lines. (well maybe, might actually slow it down who knows)

I disagree with using goto statements, it limits the scope of the case select statement and u cant have two simulated case/selects with the same labels in them else your asking for a duplicate goto/label found error.


Link Copied to Clipboard