In the 1996 reference manual HyperCard Script Language Guide: The HyperTalk Language, there is a section about nested if
structures, but there doesn't seem to be anything about switch
-like statements. Is there a switch
statement-type selection control mechanism in HyperTalk? And if not, is there a good workaround, beside creating a very large nested if
structure?

- 483
- 1
- 5
- 12
2 Answers
There is no native switch (or case) statement syntax.
If you want to identify your selected case by an integer, you can just put all the possible choices into a single variable as an array, then refer to e.g. item [number] of [variable]

- 1,457
- 1
- 9
- 26
HyperCard does not have a switch/case construct. If you want to do lots of conditional queries, you have to do nested if/then/else like
if marriedStatus = "Married" then
GetMarriedInfo
else if marriedStatus = "Single" then
GetSingleInfo
else
GetNeitherInfo
end if
(This is a chain of multi-line if
statements with single-line else
statements containing another multi-line if
)
If you just want to look up values that are indexed by a number, you can use HyperTalk's support for text items to put your values into a text field or variable, then look up those using chunk syntax:
put 2 into theNumber -- number that needs to be mapped to a choice
set the itemDelimiter to "|" -- default is comma
put "Jeanne|Sioux|Jacqueline" into choices
put item theNumber of choices into foundChoice
You can also use this together with the do
command to call different handlers, like in:
do "Get" & marriedStatus & "Info"
which will take whatever the marriedStatus
variable contains (e.g. "Single"
) and then builds the text "GetSingleInfo"
from it, and then has do call that, which calls the command handler of that name in the current handler. But note that this only works if you have a fixed set of values in marriedStatus, because otherwise your script will abort because "GetI don't careInfo"
is not a valid command handler name (not to mention you never defined a handler of that name in your script).
For what it's worth, SuperCard added a switch statement (and most HyperCard clones inherited that):
switch marriedStatus
case "Married"
GetMarriedInfo
exit switch
case "Single"
GetSingleInfo
exit switch
case true -- default case
GetNeitherInfo
exit switch
end switch
You can have more complex expressions in a case, basically it just compares the switched value with the case value.

- 8,532
- 36
- 58