Jump to content

Determine whether a variable exists vs. is empty


This topic is 3060 days old. Please don't post here. Open a new topic instead.

Recommended Posts

I need to determine whether a variable exists as distinct from whether it is empty.  From what i can tell, if i access a non-existent variable, it returns empty.  If $$foo doesn't exist, IsEmpty($$foo) returns true.  But If $$foo exists and is empty, then IsEmpty($$foo) also returns true.  How can I distinguish an existing but empty $$foo from a non-existant $$foo?  Thanks,

Link to comment
Share on other sites

Quote

"An empty variable does not exist."

Sort of.

Declare $$foo, with a value.

Use variable watcher (or script) to set $$foo to empty

$$foo is still visible in the variable watcher.

You can even close the variable watcher and open it again, and still see $$foo and its (now empty) value.

But I agree 100% with your point. Treat it as not-existing if empty.

Foo init.png

Edit foo.png

Clear foo.png

No foo.png

Link to comment
Share on other sites

1 hour ago, BruceR said:

Use variable watcher (or script) to set $$foo to empty

$$foo is still visible in the variable watcher.

That's not what I observe. I have Script A to set a $$variable to a value, and Script B to set it to empty. Watching the Current tab of the Data Viewer, the $$variable comes up when Script A runs and is taken off the board when Script B runs.

Link to comment
Share on other sites

I would guess there is in implicit "unset" type function that destroys the variable when it gets cleared via a script, but that behavior is not included when manually setting variables using the data viewer. The expected behavior is that setting a variable to "" will destroy it.

Link to comment
Share on other sites

It's also kind of interesting to create a couple of caclulations in the data viewer Watch panel:

Let( $$TESTMODE = "" ; "" )

Let( $$TESTMODE = 0 ; "" )

Whether or not the variable appears in the Current panel will depend on the order of the calcs in the Watch panel.

Be that as it may, I was able to replicate Bruce's result by manually clearing the variable in the data viewer.

Link to comment
Share on other sites

11 minutes ago, Mike Duncan said:

that behavior is not included when manually setting variables using the data viewer

 

10 minutes ago, Fitch said:

I was able to replicate Bruce's result by manually clearing the variable in the data viewer

This could be a feature of the data viewer. If you manually set the variable to empty, you may also wish to set it to something else later - so the data viewer keeps a spot for it. I don't think it has implications beyond that. It's sort of like "if a tree falls in a forest, and only the data viewer is around to hear it".

Edited by comment
  • Like 1
Link to comment
Share on other sites

Data Viewer can throw off perceptions in this way (and a few others).  Just running a script, the empty global variable disappears but if you've copied it to watch tab, it will stick around (only in the watch tab) even after script end.

I like that analogy, Michael.  :-)

 

And I mean setting it via script to empty - it still sticks around.  It doesn't have to be manually cleared.

Link to comment
Share on other sites

A little more detail. 

If I set up this expression in data viewer, evaluate it, then cancel, 

(so my expression is not stored in WATCH)

Let( [
N = 1
; $$foo1 = 1
; $$foo2 = 2
; $$foo3 = 3
/*
; $$foo1 = ""
; $$foo2 = ""
; $$foo3 = ""
*/
];
N
)

I get the first of the attached results.

If I eliminate the comment markers and evaluate again, cancel ( so the expression is not stored in WATCH) then the empty values are declared, - see second screenshot.

 

 

current.png

declare empty.png

Link to comment
Share on other sites

15 minutes ago, LaRetta said:

the empty global variable disappears but if you've copied it to watch tab, it will stick around (only in the watch tab) even after script end.

I don't think that applies here. The Watch tab keeps track of expressions, not of variables.

 

13 minutes ago, BruceR said:

If I eliminate the comment markers and evaluate again, cancel ( so the expression is not stored in WATCH) then the empty values are declared, - see second screenshot.

I am looking at your second screenshot and I don't see them.

Edited by comment
Link to comment
Share on other sites

I guess so. We may want to come up with consistent terminology.

My attempt to summarize:

If a variable is declared to hold a value (by script, Dataviewer watch statement, or whatever); then you can see it in the Current tab of Data Viewer.

In Data Viewer, you can even clear the value; but the variable name will persist, and in data viewer you can go back and put a value in it.

That is, if nothing else ( WATCH expression, script, layout object expression re-declares it).

However, if a variable declaration statement ( WATCH expression, script step, layout-based variable declaration, etc) sets (declares? erases?) the value to empty, when you go back to Data Viewer CURRENT tab you won't see the variable name at all.

Link to comment
Share on other sites

I don't disagree, I just think that for all practical purposes it's sufficient to say that setting a variable to empty erases it .

Even if we were to concede the possibility of an empty variable "existing" somehow, there is no way I know of to distinguish it from a variable that doesn't exist (to answer the original question). So this is just a matter of "do you believe that empty variables exist?".

Link to comment
Share on other sites

This topic is 3060 days old. Please don't post here. Open a new topic instead.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.