-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswitch.ado
44 lines (37 loc) · 1.23 KB
/
switch.ado
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
*! version 1.0.2 26nov2009 Rodrigo Martell
version 10.1
program define switch
syntax anything, CAses(str) VALues(str)
/* determine number of cases */
local no_cases=wordcount("`cases'")
/* determine number of values */
local no_values=wordcount("`values'")
/* Error trapping */
/* complain if number of cases and values don't equate. */
if `no_cases'!=`no_values'|`no_cases'==0 | `no_values'==0{
di "The number of cases and values doesn't match, or you haven't entered any cases or values."
exit
}
/* complain if more than one arguments are passed */
if wordcount("`anything'")>1{
di "You may only supply one case to match to a value at a time."
exit
}
/* Execute switching */
local result=0
forvalues i=1/`no_cases'{
tokenize `cases'
if "`anything'"=="``i''"{
tokenize `values'
global switch_return="``i''"
local result=1
di "switch matched `anything' to a value of $switch_return"
exit
}
}
/* report when finished */
if `result'==0{
global switch_return="no_swatch"
di "switch found no matching value for `anything'"
}
end