M Tutorial (part 3/6)
$SELECT(<condition>:<expression>[,
])
or $S(<condition>:<expression>[,
])
Within this function, you place <condition>s together with what happens if
the condition is true (the <expression>). This function processes conditions
from left to right and upon encountering a valid condition it returns the expression and
stops.
INTFN111Þ;Program demonstrates intrinsic functions - Chris Bonnici - Aug 1997
Þ;M Web Magazine @ http://geocities.datacellar.net/SiliconValley/7041/mwm.html
Þ;You are using this program at your own risk
Þ;
ÞN STATUS
ÞW #
ÞR /CUP(1,1),/EL(2),"Enter Status: ",STATUS#1
ÞQ:STATUS=""
ÞW
/CUP(1,15),$S(STATUS="S":"Single",STATUS="M":"Married",STATUS="D":"Divorced")
ÞQ
D R
Y
R
U
N |
After the READ instruction, variable
STATUS will contain a single letter. If it is null the program quits. If
STATUS="S" the phrase "Single" is returned, otherwise if it is
"M" the phrase "Married" is returned otherwise if it is "D"
the phrase "Divorced" is returned. |
Our code is flawed (design error) because it does not cater for people whose
partner has died. Without altering any code, try typing in a W in status and see what
happens.
Fix this bug
You should be aware that when using the $SELECT it will fail if all conditions fail. It
helps to associate the right hand bracket with a brick wall. If you get there, youre
in for a crash.
INTFN112Þ;Program demonstrates intrinsic functions - Chris Bonnici - Aug 1997
Þ;M Web Magazine @ http://geocities.datacellar.net/SiliconValley/7041/mwm.html
Þ;You are using this program at your own risk
Þ;
ÞN STATUS
ÞW #
ÞR /CUP(1,1),/EL(2),"Enter Status: ",STATUS#1
ÞQ:STATUS=""
ÞW
/CUP(1,15),$S(STATUS="S":"Single",STATUS="M":"Married",STATUS="D":"Divorced",
"W":"Widower",1:"N/A")
ÞQ
We fix this problem by
catering for the "W" status and by introducing a trap all check in the form of 1:"N/A"
situated at the rightmost position. Since a 1 is the representation of true, if everything
else fails the 1 automatically works out to be true and will process the expression tied
to it.
Therefore as a rule of thumb, when using $SELECT, you can either:
- ensure that no illegal entry can filter into a variable (by refusing anything invalid).
With this approach you can do away with the trap all 1:<expression> clause.
- Insert the trap all clause.
Modify INTFN112
so that you can do away with the trap all part of $SELECT.
Another important consideration of this intrinsic function is that it processes
conditions left to right and upon encountering a true condition it stops processing. We
have modified INTFN112 to demonstrate this.
INTFN113Þ;Program demonstrates intrinsic functions - Chris Bonnici - Aug 1997
Þ;M Web Magazine @ http://geocities.datacellar.net/SiliconValley/7041/mwm.html
Þ;You are using this program at your own risk
Þ;
ÞN STATUS
ÞW #
ÞR /CUP(1,1),/EL(2),"Enter Status: ",STATUS#1
ÞQ:STATUS=""
ÞW /CUP(1,15),$S(1:"N/A",STATUS="S":"Single",STATUS="M":"Married",
STATUS="D":"Divorced","W":"Widower")
ÞQ
This code will always give you a N/A because
since 1 is always true, the $SELECT outputs that expression and quits. A flow diagram
should help you understand how it this command works.
Recall INTFN110s CARDFACE and PLAYCARD functions. We can now convert these to
operate using the $SELECT. The resulting code will be more compact without loosing any
readability and maintainability.
Þ; The Card number is converted to the appropriate type and played. 1 - 13 Hearts, 14
- 26 Spades, 27 - 39 Clubs, 40 - 52 Diamonds
CARDFACE(CARDNUM)ÞN FACE
ÞS FACE=CARDNUM#13
ÞS FACE=$S(FACE=0:"K",FACE=12:"Q",FACE=11:"J",1:FACE)
ÞS
FACE=FACE_$S(CARDNUM<14:"H",CARDNUM<27:"S",CARDNUM<40:"C",1:"D")
ÞQ FACE
Þ;*** EOR ***
Þ; Convert a card face to its numeric equivalent
PLAYCARD(CARD)ÞN FACE,NUM
Þ; Convert Face to Card Number
ÞS NUM=$E(CARD,1,$L(CARD)-1),FACE=$E(CARD,$L(CARD))
ÞS
NUM=$S(NUM="K":13,NUM="Q":12,NUM="J":11,1:NUM)
ÞS
FACE=$S(FACE="H":0,FACE="S":12,FACE="C":25,FACE="D":39)
ÞS NUM=NUM+FACE
ÞQ NUM
We can go one step further by combining the lines shown in
red:
ÞS
NUM=$S(NUM="K":13,NUM="Q":12,NUM="J":11,1:NUM)+
$S(FACE="H":0,FACE="S":12,FACE="C":25,FACE="D":39)
Continued...
E&OE
|