Hi – I wrote a mata function called -pr_exists()- that tests if a mata object exists. See below. My intent was to include it in programs to test if something exists before issuing a -mata drop _object_ - command, but it turns out that -mata _stuff_ - commands do not work inside if{} or do{} blocks of code. I understand why. But, how can I solve this problem? Thx! -- P
. mata
------------------------------------------------- mata (type end to exit) -----
:
: void function saySomething(string scalar x)
> {
> printf("%s",x)
> return
> }
: saySomething("I said this")
I said this
:
:
: real scalar function pr_exists(string scalar x)
> {
> /* tests and returns 1/0 if mata object exists */
> return(findexternal(x)!=NULL)
> }
: pr_exists("x")
0
: pr_exists("func()")
0
: pr_exists("saySomething()")
1
:
: if (pr_exists("saySomething()")) x=6
> x
6
:
: if (pr_exists("saySomething()")) mata drop saySomething()
invalid expression
r(3000);
:
: end
-------------------------------------------------------------------------------
. exit
. mata
------------------------------------------------- mata (type end to exit) -----
:
: void function saySomething(string scalar x)
> {
> printf("%s",x)
> return
> }
: saySomething("I said this")
I said this
:
:
: real scalar function pr_exists(string scalar x)
> {
> /* tests and returns 1/0 if mata object exists */
> return(findexternal(x)!=NULL)
> }
: pr_exists("x")
0
: pr_exists("func()")
0
: pr_exists("saySomething()")
1
:
: if (pr_exists("saySomething()")) x=6
> x
6
:
: if (pr_exists("saySomething()")) mata drop saySomething()
invalid expression
r(3000);
:
: end
-------------------------------------------------------------------------------
. exit
Comment