Register to post in forums, or Log in to your existing account
 

 Related 
Contents
Conditionals
  #CASE
  #IF
  %pat
  Conditionals
  Expressions
Command Reference
  #nnn
  #ABORT
  #ACTION
  #ADD
  #ADDITEM
  #ADDKEY
  #AFK
  #ALARM
  #ALIAS
  #ALL
  #BACKUP
  #BEEP
  #BREAK
  #BUTTON
  #C-
  #C+
  #CALL
  #CAPTURE
  #CASE
  #CHARACTER
  #CLASS
  #CLOSE
  #CLR
  #CMD
  #COLOR
  #COM
  #CONDITION
  #CONFIGURE
  #CONNECT
  #CONTINUE
  #CR
  #CW
  #DBCLOSE
  #DBDELETE
  #DBFIRST
  #DBGET
  #DBLOAD
  #DBNEXT
  #DBOFFLINE
  #DBONLINE
  #DBPREV
  #DBPUT
  #DBRESET
  #DBSAVE
  #DDE
  #DEBUG
  #DEBUGFILE
  #DEFAULT
  #DELCLASS
  #DELITEM
  #DELKEY
  #DELNITEM
  #DESCRIBE
  #DIRECTION
  #DISCONNECT
  #DOOR
  #ECHO
  #ECHOPROMPT
  #EDITOR
  #ERASE
  #EVALUATE
  #EVENT
  #EXECUTE
  #EXECWIN
  #EXIT
  #FILE
  #FIND
  #FIRE
  #FORALL
  #FREEZE
  #FTP
  #FUNCTION
  #GAG
  #GAGBLOCK
  #GAGLOG
  #GAGOFF
  #GAGON
  #GAGSPACE
  #GAUGE
  #H-
  #H+
  #HELP
  #HIGHLIGHT
  #HISTLOAD
  #HISTORY
  #HISTSAVE
  #HOST
  #IF
  #IGNORE
  #IMAGE
  #INPUT
  #KEY
  #KILLALL
  #LAUNCH
  #LMAP
  #LOAD
  #LOCAL
  #LOCATION
  #LOG
  #LOOK
  #LOOP
  #LOOPDB
  #LOOPVIEW
  #LUA
  #MAKEROOM
  #MAKEWINDOW
  #MAP
  #MAPQUERY
  #MARK
  #MATH
  #MEDIA
  #MENU
  #MERGE
  #MESSAGE
  #MODULE
  #MOVE
  #MSS
  #MXP
  #MXPTRIG
  #NAME
  #NEW
  #NEWVAR
  #NODEF
  #NODIR
  #NOINPUT
  #NOMAP
  #NOOP
  #NOSAVE
  #OK
  #ONINPUT
  #PATH
  #PATHHIGH
  #PAUSE
  #PCOL
  #PICK
  #PLAY
  #PLUGIN
  #PORTAL
  #PRINT
  #PROMPT
  #PSUB
  #PUEBLO
  #PW
  #QUERY
  #QUEUE
  #RAISEEVENT
  #READ
  #RECALL
  #RECORD
  #REGEX
  #RENAME
  #REPEAT
  #RESET
  #RESUME
  #RESULT
  #RETRACE
  #RETURN
  #SAVE
  #SAY
  #SAYADD
  #SAYPROMPT
  #SCRIPT
  #SCROLL
  #SECTION
  #SEND
  #SENDGMCP
  #SENDPROMPT
  #SENDRAW
  #SENDSB
  #SESSION
  #SET
  #SETPROMPT
  #SHOW
  #SHOWARR
  #SHOWDB
  #SHOWPROMPT
  #SHOWTABLE
  #SIGNAL
  #SLOW
  #SORT
  #SQLCLOSE
  #SQLDB
  #SS
  #STATE
  #STATUS
  #STEP
  #STOP
  #STWIN
  #STYLE
  #SUBSTITUTE
  #SUSPEND
  #SWITCH
  #T-
  #T?
  #T+
  #TAB
  #TAG
  #TELEPORT
  #TEMP
  #TEMPVAR
  #THREAD
  #TIMER
  #TRACK
  #TRIGGER
  #TSET
  #TYPE
  #TZERO
  #UNALIAS
  #UNBUTTON
  #UNCLASS
  #UNDIR
  #UNGAG
  #UNKEY
  #UNEVENT
  #UNLOAD
  #UNLOC
  #UNMENU
  #UNPATH
  #UNPORTAL
  #UNSTYLE
  #UNTAB
  #UNTIL
  #UNTRIGGER
  #UNVAR
  #UPDATE
  #URL
  #VARFUNC
  #VARIABLE
  #VERBATIM
  #VERSION
  #VIEW
  #WAIT
  #WAITFOR
  #WAITSIGNAL
  #WAITTHREAD
  #WALK
  #WHILE
  #WINDOW
  #WITHLOC
  #WIZLIST
  #WRAP
  #WRITE
  #WRITELOG
  #YESNO
Related Links:
  Conditionals
  New Commands
#SWITCH [[cmud_#SW]] 
SWITCH

Syntax: #SW (expression) {command} (expression) {command} ... {else_command}

Executes a command based upon which expression is true. This is similar to a giant set of if-then-elseif-elseif...else commands. Each expression is followed by a set of commands. Each expression is tested until one is found that is true. When one is found that is true, then the commands listed just after this expression are executed. If no expressions are true and there is an extra set of commands at the end, then this final "else" command is executed.

SWITCH example

a=10
#SWITCH (@a=1) {a is one} (@a < 0) {a is negative} (@a=10) {a is ten} (@a >= 10) {a is ge ten} {nothing matched}
displays "a is ten". Notice that the @a>=10 command doesn't execute because the @a=10 expression already matched. Once an expression matches, the rest of the command is ignored.

Note that you can implement an "else" clause at the end by using an expression of "1" or true. This will cause the final value to be returned if no other expressions matched.

To use the multiline syntax in CMUD, the first expression and command must be on the same line as the #SWITCH, and each new expression and command must be on their own lines, indented from the first. For example:
Code:
#SWITCH (@a=1) {a is one}
  (@a < 0) {a is negative}
  (@a=10) {a is ten}
  (@a >= 10) {a is ge ten}
  {nothing matched}


A shortcut exists for switch commands that check a range of values of a single variable:
Code:
#switch (@a)
  (0) {a is 0}
  ("blueberries") {a is "blueberries"}
  ("") {a is an empty string}
  {nothing matched}

is equivalent to:
Code:
#switch (@a=0) {a is 0}
  (@a="blueberries") {a is "blueberries"}
  (@a="") {a is an empty string}
  {nothing matched}

 User comments 
edb6377: Tue Oct 10, 2006 3:22 am    

Example of a switch and a nested switch

Code:

#LOCAL $world1 $world2
#SWITCH (%1= "full") {
#SWITCH (%2= "full") {
      $world1 = %replace( "Rune 662, 746", ",")
      $world2 = %replace( "Xaventry 625, 461", ",")
   } (%2= "gibbous waning") {
      $world1 = %replace( "Rune 662, 746", ",")
      $world2 = %replace( "Lowangen 1177, 384", ",")
   } (%2= "gibbous waxing") {
      $world1 = %replace( "Rune 662, 746", ",")
      $world2 = %replace( "Ethereal Void ?", ",")
   } (%2= "half waxing") {
      $world1 = %replace( "Rune 662, 746", ",")
      $world2 = %replace( "Templeton 1674, 541", ",")
   } (%2= "half waning") {
      $world1 = %replace( "Rune 662, 746", ",")
      $world2 = %replace( "CTF Arena 967, 699", ",")
   } (%2= "new") {
      $world1 = %replace( "Rune 662, 746", ",")
      $world2 = %replace( "Dungeon Deceit 1015, 1116", ",")
   } (%2= "crescent waning") {
      $world1 = %replace( "Rune 662, 746", ",")
      $world2 = %replace( "Sigil 502, 1281", ",")
   } (%2= "crescent waxing") {
      $world1 = %replace( "Rune 662, 746", ",")
      $world2 = %replace( "Maldra's Keep 1279, 1053", ",")
   }
} (%1="crescent waxing") {
  #SWITCH (%2= "gibbous waning") {
      $world1 = %replace( "Dungeon Deceit 1015, 1116", ",")
      $world2 = %replace( "Templeton 1674, 541", ",")
   } (%2= "crescent waning") {
      $world1 = %replace( "CTF Arena 967, 699", ",")
      $world2 = %replace( "Xaventry 625, 461", ",")
   } (%2= "crescent waxing") {
      $world1 = %replace( "Dungeon Deceit 1015, 1116", ",")
      $world2 = %replace( "CTF Arena 697, 699", ",")
   } (%2= "new") {
      $world1 = %replace( "CTF Arena 967, 699", ",")
      $world2 = %replace( "Lowangen 1177, 384", ",")
   } (%2= "half waxing") {
      $world1 = %replace( "Dungeon Deceit 1015, 1116", ",")
      $world2 = %replace( "Sigil 502, 1281", ",")
   } (%2="half waning") {
      $world1 = %replace( "Dungeon Deceit 1015, 1116", ",")
      $world2 = %replace( "Rune 662, 746", ",")
   } (%2= "gibbous waxing") {
      $world1 = %replace( "Seelie Castle ?", ",")
      $world2 = %replace( "Templeton 1674, 541", ",")
   } (%2= "full") {
      $world1 = %replace( "CTF Arena 697, 699", ",")
      $world2 = %replace( "Maldra's Keep 1279, 1053", ",")
   }
} (%1= "gibbous waning") {
   #SWITCH (%2= "full") {
      $world1 = %replace( "Xaventry 625, 461", ",")
      $world2 = %replace( "Lowangen 1177, 384", ",")
   } (%2= "gibbous waning") {
      $world1 = %replace( "Xaventry 625, 461", ",")
      $world2 = %replace( "Arcane 967, 699", ",")
   } (%2= "gibbous waxing") {
      $world1 = %replace( "Xaventry 625, 461", ",")
      $world2 = %replace( "Decara 1538, 430", ",")
   } (%2= "half waxing") {
      $world1 = %replace( "Xaventry 625, 461", ",")
      $world2 = %replace( "Rune 662, 746", ",")
   } (%2= "half waning") {
      $world1 = %replace( "Xaventry 625, 461", ",")
      $world2 = %replace( "Sigil 502, 1281", ",")
   } (%2= "new") {
      $world1 = %replace( "Xaventry 625, 461", ",")
      $world2 = %replace( "Maldra's Keep 1279, 1053", ",")
   } (%2= "crescent waning") {
      $world1 = %replace( "Xaventry 625, 461", ",")
      $world2 = %replace( "Dungeon Deceit 1015, 1116",    ",")
   } (%2= "crescent waxing") {
      $world1 = %replace( "Xaventry 625, 461", ",")
      $world2 = %replace( "Templeton 1674, 541", ",")
   }
} (%1="Half Waning") {
   #SWITCH (%2= "full") {
      $world1 = %replace( "Lowangen 1177, 384", ",")
      $world2 = %replace( "CTF Arena 967, 699", ",")
   } (%2= "gibbous waning") {
      $world1 = %replace( "Lowangen 1177, 384", ",")
      $world2 = %replace( "Sigil 502, 1281", ",")
   } (%2= "gibbous waxing") {
      $world1 = %replace( "Lowangen 1177, 384", ",")
      $world2 = %replace( "Decara 1538, 430", ",")
   } (%2= "half waxing") {
      $world1 = %replace( "Lowangen 1177, 384", ",")
      $world2 = %replace( "Xaventry 625, 461", ",")
   } (%2="half waning") {
      $world1 = %replace( "Lowangen 1177, 384", ",")
      $world2 = %replace( "CTF Arena 967, 699", ",")
   } (%2= "new") {
      $world1 = %replace( "Lowangen 1177, 384", ",")
      $world2 = %replace( "Templeton 1674, 541", ",")
   } (%2= "crescent waning") {
      $world1 = %replace( "Lowangen 1177, 384", ",")
      $world2 = %replace( "Maldra's Keep 1279, 1053", ",")
   } (%2= "crescent waxing") {
      $world1 = %replace( "Lowangen 1177, 384", ",")
      $world2 = %replace( "Rune 662, 746", ",")
   }
} (%1="cresent waning") {
   #SWITCH (%2= "full") {
      $world1 = %replace( "Arcane 967, 699", ",")
      $world2 = %replace( "Sigil 502, 1281", ",")
   } (%2= "gibbous waning") {
      $world1 = %replace( "Arcane 967, 699", ",")
      $world2 = %replace( "Dungeon Deceit 1015, 1116", ",")
   } (%2= "gibbous waxing") {
      $world1 = %replace( "Sigil 502, 1281", ",")
      $world2 = %replace( "Decara 1538, 430", ",")
   } (%2= "half waxing") {
      $world1 = %replace( "CTF Arena 967, 699", ",")
      $world2 = %replace( "Lowangen 1177, 384", ",")
   } (%2="half waning") {
      $world1 = %replace( "CTF Arena 967, 699", ",")
      $world2 = %replace( "Maldra's Keep 1279, 1053", ",")
   } (%2= "new") {
      $world1 = %replace( "Arcane 967, 699", ",")
      $world2 = %replace( "Rune 662, 746", ",")
   } (%2= "crescent waning") {
      $world1 = %replace( "CTF Arena 967, 699", ",")
      $world2 = %replace( "Dungeon Deceit 1015, 1116", ",")
   } (%2= "crescent waxing") {
      $world1 = %replace( "CTF Arena 967, 699", ",")
      $world2 = %replace( "Xaventry 625, 461", ",")
   }
} (%1="new") {
   #SWITCH (%2= "gibbous waning") {
      $world1 = %replace( "Sigil 502, 1281", ",")
      $world2 = %replace( "Maldra's Keep 1279, 1053", ",")
   } (%2= "half waxing") {
      $world1 = %replace( "Sigil 502, 1281", ",")
      $world2 = %replace( "Arcane 967, 699", ",")
   } (%2= "crescent waning") {
      $world1 = %replace( "Sigil 502, 1281", ",")
      $world2 = %replace( "Rune 662, 746", ",")
   } (%2= "crescent waxing") {
      $world1 = %replace( "Sigil 502, 1281", ",")
      $world2 = %replace( "Xaventry 625, 461", ",")
   } (%2= "new") {
      $world1 = %replace( "Sigil 502, 1281", ",")
      $world2 = %replace( "Lowangen 1177, 384", ",")
   } (%2="half waning") {
      $world1 = %replace( "Sigil 502, 1281", ",")
      $world2 = %replace( "Templeton 1674, 541", ",")
   } (%2= "gibbous waxing") {
      $world1 = %replace( "Decara 1538, 430", ",")
      $world2 = %replace( "Xaventry 625, 461", ",")
   } (%2= "full") {
      $world1 = %replace( "Sigil 502, 1281", ",")
      $world2 = %replace( "Dungeon Deceit 1015, 1116", ",")
   }
} (%1="cresent waxing") {
   #SWITCH (%2= "gibbous waning") {
      $world1 = %replace( "Dungeon Deceit 1015, 1116", ",")
      $world2 = %replace( "Rune 662, 746", ",")
   } (%2= "crescent waning") {
      $world1 = %replace( "Maldra's Keep 1279, 1053", ",")
      $world2 = %replace( "Lowangen 1177, 384", ",")
   } (%2= "crescent waxing") {
      $world1 = %replace( "Dungeon Deceit 1015, 1116", ",")
      $world2 = %replace( "Sigil 502, 1281", ",")
   } (%2= "new") {
      $world1 = %replace( "Dungeon Deceit 1015, 1116", ",")
      $world2 = %replace( "Arcane ?", ",")
   } (%2= "half waxing") {
      $world1 = %replace( "Maldra's Keep 1279, 1053", ",")
      $world2 = %replace( "Dungeon Deceit 1015, 1116", ",")
   } (%2="half waning") {
      $world1 = %replace( "Maldra's Keep 1279, 1053", ",")
      $world2 = %replace( "Xaventry 625, 461", ",")
   } (%2= "gibbous waxing") {
      $world1 = %replace( "Seahag Isle 1752, 984", ",")
      $world2 = %replace( "Lowangen 1177, 384", ",")
   } (%2= "full") {
      $world1 = %replace( "Dungeon Deceit 1015, 1116", ",")
      $world2 = %replace( "Templeton 1674, 541", ",")
   }
} (%1="gibbous waxing") {
   #SWITCH (%2= "gibbous waning") {
      $world1 = %replace( "Templeton 1674, 541", ",")
      $world2 = %replace( "Xaventry 625, 461", ",")
   } (%2= "crescent waning") {
      $world1 = %replace( "Templeton 1674, 541", ",")
      $world2 = %replace( "Arcane 967, 699", ",")
   } (%2= "crescent waxing") {
      $world1 = %replace( "Templeton 1674, 541", ",")
      $world2 = %replace( "Dungeon Deceit 1015, 1116", ",")
   } (%2= "new") {
      $world1 = %replace( "Templeton 1674, 541", ",")
      $world2 = %replace( "Sigil 502, 1281", ",")
   } (%2= "half waxing") {
      $world1 = %replace( "Templeton 1674, 541", ",")
      $world2 = %replace( "Maldra's Keep 1279, 1053", ",")
   } (%2="half waning") {
      $world1 = %replace( "Xaventry 625, 461", ",")
      $world2 = %replace( "Sigil 502, 1281", ",")
   } (%2= "gibbous waxing") {
      $world1 = %replace( "Hotel Hello ?", ",")
      $world2 = %replace( "Rune 662, 746", ",")
   } (%2= "full") {
      $world1 = %replace( "Templeton 1674, 541", ",")
      $world2 = %replace( "Rune 662, 746", ",")
   }
}
#echo %ansi( hi, magenta)Worldgate from %ansi( hi, blue)$world1 %ansi( hi, magenta)to %ansi( hi, blue)$world2
Arde: Tue Nov 13, 2007 9:28 pm    

As in CMUD 2.11, multiline syntax for the #SWITCH command that check a range of values of a single variable may not have the first expression and command on the same line as the #SWITCH. The following compiles and works perfectly:

Code:

  #SWITCH ($Flag)
    ("")
      {
        // No flags specified.
        ....................
      }
    ("+")
      {
        // "+" flag
        .............
      }
    ("-")
      {
        // "-" flag
        ............
      }
    {
      //Unknown flag
    }
Viewer Comments [2 - Post your comments]

Jump to:  

© 2009 Zugg Software. Hosted by Wolfpaw.net