Greetings,
I am fairly new to Mata, so I wonder if I'm making the following too complicated. I have a Mata program that reads in a spreadsheet and parses it in various ways. One thing it needs to do is process a string from the spreadsheet that contains Stata-style options; it needs to split one option from the rest. If this were pure Stata, this is easy:
But I need to do this in the middle of a bunch of Mata code. Here's what I've come up with. This works, but feels like it's more circuitous than need be?
I am fairly new to Mata, so I wonder if I'm making the following too complicated. I have a Mata program that reads in a spreadsheet and parses it in various ways. One thing it needs to do is process a string from the spreadsheet that contains Stata-style options; it needs to split one option from the rest. If this were pure Stata, this is easy:
Code:
local 0 , `option_string' syntax , [ MYopt(numlist) * ] local option_string `options' di "I just parsed numlist: (`myopt')"
Code:
program pull_option_stata
args option_string opt_to_pull result_local
local 0 , `option_string'
syntax , [ `opt_to_pull' * ]
* Mata can only reach into namespace of caller, so:
c_local options `options'
c_local `result_local' ``result_local''
end
mata
string scalar pull_option(string scalar options, string scalar opt_to_pull, string scalar result_local) {
stata(`"pull_option_stata ""'+options+`"" ""'+opt_to_pull+`"" ""'+result_local+`"""')
display("Remaining options: ["+st_local("options")+"]")
display("and now "+result_local+"==[" + st_local(result_local) + "]")
options = st_local("options")
return(st_local(result_local))
}
the_options = "this that myopt(1/3)"
myopt = pull_option(the_options,"MYopt(numlist)","myopt")
display("after pull:")
the_options
myopt

Comment