Stata: Ado file syntax - stata

I am working with an .ado file named flow. If the user types flow i I want one if statement to run. If the user types flow e, I want another if statement to run.
How do I do this?

Many readers of this forum expect to see some code that you tried....
program flow
version 8 // will work on almost all Stata in current use
gettoken what garbage : 0
if "`what'" == "" | "`garbage'" != "" | !inlist("`what'", "e", "i") {
di as err "syntax is flow e or flow i"
exit 198
}
if "`what'" == "e" {
<code for e>
}
else if "`what'" == "i" {
<code for i>
}
end
The last if condition is redundant as we've already established that the user typed e or i. Edit it out according to taste.

Given your comment on the answer by #NickCox, I assume you tried something like this:
program flow
version 8
syntax [, i e]
if "`i'`e'" == "" {
di as err "either the i or the e option needs to be specified"
exit 198
}
if "`i'" != "" & "`e'" != "" {
di as err "the i and e options cannot be specified together"
exit 198
}
if "`e'" != "" {
<code for e>
}
if "`i'" != "" {
<code for i>
}
end
After that you call flow like this: flow, i or flow, e. Notice the comma, this is now necessary (but not in the command by #NickCox) because you made them options.

If you want i and e to be mutually exclusive options, then this is yet another alternative:
program flow
version 8
capture syntax , e
if _rc == 0 { // syntax matched what was typed
<code for e>
}
else {
syntax , i // error message and program exit if syntax is incorrect
<code for i>
}
end
If the code in each branch is at all long, many would prefer subprograms for each case as a matter of good style, but that would be consistent with the sketch here. Note that in each syntax statement the option is declared compulsory.
The effect of capture is this: errors are not fatal, but are "eaten" by capture. So you need to look at the return code, accessible in _rc. 0 for _rc always means that the command was successful. Non-zero always means that the command was unsuccessful. Here, and often elsewhere, there are only two ways for the command to be right, so we don't need to know what _rc was; we just need to check for the other legal syntax.
Note that even my two answers here differ in style on whether a user typing an illegal command gets an informative error message or just "invalid syntax". The context to this is an expectation that every Stata command comes with a help file. Some programmers write on the assumption that the help file explains the syntax; others want their error messages to be as helpful as possible.

Related

nil check and error check in single if statement

I was attempting to do a nil check and then a method error check in a single if statement. Since the OR operator won't check any other conditions if the first one is true, I figured I could do something like this, to avoid nil pointer crashes and handle errors together:
if (uc.registry == nil) || (serviceName, err = uc.registry.GetServiceName(itemKind, key); err != nil) {}
This gives a syntax error, as it seems to try comparing the result of the first condition with the string value of serviceName.
Is it possible to do this?
Conditions must appear on the right-hand side of the ; if it is present, per the documentation. It is a shorthand for simple cases like error checks and map lookups. What you're doing would require two if statements.

F# if statement function with parameter syntax

The issue is: I cannot figure out what the error is refering to when it diplays
Here is the error:
source_file.fs(10,5): error FS0010: Unexpected keyword 'if' in binding. Expected '=' or other token.
And I've been researching this error and proper syntax for a good while.
Now what I want to do, I hope, is obvious from the general look of the program.
Knowing the correct syntax would be great as microsofts docs are not great.
Seeing as this is case, I just don't understand what could be wrong.
open System
let one = "one"
let two = "two"
if oneortwo one then printfn one + " 1"
else printfn two + " 2"
let oneortwo(a : string)
if a = "one" then return true
elif a = "two" then return false
return false
F# is an expression based language, which means that everything has a value (returns something). F# is also statically typed, so everything returned is of a specific type.
Since everything is an expression, the return keyword is not used. The final expression in a function body is the returned value.
This goes also for if ... then ... else: every branch must return a value and be of the same type.
The correct syntax for your function is
let oneortwo a =
if a = "one" then true
else false
An excellent source of learning F# is Scott Wlaschin's site F# for fun and profit

Eiffel: classical typecasting switch structure with attached and inspect

What is the best practice to do something such as
local
l_pet: ANIMAL
do
l_pet := catch_it_from_the_sky
inspect l_pet
when attached {DOG} l_pet as l_dog
l_dog.eat (meat)
when attached {FISH} l_pet as l_fish
l_fish.eat (plants)
else
io.put_string ("Strange animal how do I feed him???")
end
do
the compiler is complaining with the attached after when...
Update: why such a need?
because it just happened me to mess up with repeated copy-paste which is what a language tries to help avoiding. In the above case, the l_pet is written one time, with a N times if/else I'd have to write it as much times as ifs...
An inspect statement allows for checking if an expression has a specific value, and can be applied to expressions of integral types (such as INTEGER_64, CHARACTER_32 or NATURAL_8):
inspect age
when 6 .. 16 then ...
when 18 then ...
when 80, 90 then ...
...
end
For discriminating over object types, conditional instructions are used:
if attached {DOG} pet as dog then
dog.eat (meat)
elseif attached {FISH} pet as fish then
fish.eat (plants)
else
io.put_string ("Strange animal how do I feed him???")
end
In a multi-branch instruction
inspect exp when ... then ... else ... end
The exp expression needs to be a character or an integer expression.
In your given example I don't see the need to do that Object-Test,
but if you need to do something like that you need to use the conditional instruction.
if ... then
...
elseif ... then
...
else
...
end

What is the "select when" syntax for?

Experimenting with the language I've found that select is defined in the global scope and its precedence is higher than local variables.
def example(select)
puts select
end
example 3
# Syntax error in eval:3: unexpected token: end (expecting when, else or end)
So experimenting with select step by step I get this:
select 1 end
# Syntax error in eval:3: unexpected token: end (expecting when, else or end)
and then
select when 1 end
# Syntax error in eval:1: invalid select when expression: must be an assignment or call
then
select when x = 1 end
# Syntax error in eval:1: invalid select when expression: must be an assignment or call
then
select when x
# Syntax error in eval:1: unexpected token: EOF (expecting ',', ';' or '
I'll skip ahead a few steps as you should have an idea of how I've come to my question…
select when x;
else y
end
# Error in line 1: undefined local variable or method 'x_select_action'
and lastly
x_select_action = 4
select when x;
else y
end
# Error in line 3: undefined method 'x_select_action' (If you declared 'x_select_action' in a suffix if, declare it in a regular if for this to work. If the variable was declared in a macro it's not visible outside it)
So there is this keyword in the language which precedes local variables precedence and I don't know what it's for. But apparently it looks for x_select_action when x is given as a when clause. What is this select for and how is it meant to be used?
Searching online I see select defined on Enumerable, Hash, Channel, and Array… but at first glance these don't seem to be it.
Thanks for the help!
It's similar to Go's select: https://tour.golang.org/concurrency/5
But it still needs some tweaks to be finished, that's why there are no docs about it yet.

Autohotkey if statement not working, no error msg

I'm trying to use autohotkey to gather a chuck of data from a website and then click a certain spot on the website depending on what the text is. I'm able to get it to actually pick up the value but when it comes to the if statement it won't seem to process and yields no error message. Here is a quick sample of my code, there is about 20 if statement values so for brevity sake I've only included a few of the values.
GuessesLeft = 20
Errorcount = 0
;triple click and copy text making a variable out of the clipboard
;while (GuessesLeft!=0) part of future while loop
;{ part of future while loop
click 927,349
click 927,349
click 927,349
Send ^c
GetValue = %Clipboard%
if ( GetValue = "Frontal boss")
{
click 955,485
Guessesleft -= 1
}
else if ( GetValue = "Supraorbital Ridge")
{
click 955,571
Guessesleft -= 1
}
;....ETC
else
{
Errorcount += 1
}
;} part of future while loop
Any tips on what I might be doing wrong. Ideally I'd use a case statement but AHK doesn't seem to have them.
Wait a second -- you are triple clicking to highlight a full paragraph and copying that to the clipboard and checking to see if the entirety of the copied portion is the words in the if statement, right? And your words in the copied portion have quotes around them? Probably you will have to trim off any trailing spaces and/or returns:
GetValue = % Trim(Clipboard)
If that doesn't work, you may even have to shorten the length of the copied text by an arbitrary character or two:
GetValue = % SubStr(Clipboard, 1, (StrLen(Clipboard)-2))
Now, if I am wrong, and what you are really looking for is the words from the if statement wherever they may be in a longer paragraph -- and they are not surrounded by quotes, then you will want something like:
IfInString, Clipboard, Frontal boss
Or, if the quotes ARE there,
IfInString, Clipboard, "Frontal boss"
Hth,