Jump to content
View in the app

A better way to browse. Learn more.

FMForums.com

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Determine whether a variable exists vs. is empty

Featured Replies

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,

An empty variable does not exist.

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

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.

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.

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.

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

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.

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

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

Yes. That is the correct result. They do no exist.

1 minute ago, BruceR said:

Yes. That is the correct result. They do no exist.

Okay then. I guess you meant to say they were erased, instead of declared.

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.

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?".

Yes. Empty variables don't exist.

Create an account or sign in to comment

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.