How to test whether a variable is empty
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
-
- Posts: 222
- Joined: Thu Jul 01, 2010 11:50 am
How to test whether a variable is empty
I've updated this post in light of the solution.
If a variable hasn't been used, you can't test for empty. This isn't documented, at least not that I could find.
For example:
put undefinedVariable is empty -- returns false because it's evaluating the string "undefinedVariable"
The number of words in undefinedVariable? Nope, that's 1 ("undefinedVariable").
Put it in a field and check whether the field is empty? Nope, "put undefinedVariable in field output" results in the field containing the word "undefinedVariable".
"the value of undefinedVariable" returns the string "undefinedVariable".
You can test whether "it" is empty. But if you "put undeclaredVariable into it", that puts the string "undeclaredVariable" into it, so it is not empty.
There is no function "isEmpty()".
Updated with solution:
You can test whether a variable is empty as long as you use it first, by either putting a value in it at some point, or declaring it ("local tVariable").
If the variable either contains a string or is empty (i.e., it can't contain a number), you can add 0 to it and then test (i.e., if myVar + 0 is empty...).
If a variable hasn't been used, you can't test for empty. This isn't documented, at least not that I could find.
For example:
put undefinedVariable is empty -- returns false because it's evaluating the string "undefinedVariable"
The number of words in undefinedVariable? Nope, that's 1 ("undefinedVariable").
Put it in a field and check whether the field is empty? Nope, "put undefinedVariable in field output" results in the field containing the word "undefinedVariable".
"the value of undefinedVariable" returns the string "undefinedVariable".
You can test whether "it" is empty. But if you "put undeclaredVariable into it", that puts the string "undeclaredVariable" into it, so it is not empty.
There is no function "isEmpty()".
Updated with solution:
You can test whether a variable is empty as long as you use it first, by either putting a value in it at some point, or declaring it ("local tVariable").
If the variable either contains a string or is empty (i.e., it can't contain a number), you can add 0 to it and then test (i.e., if myVar + 0 is empty...).
Last edited by MichaelBluejay on Thu Oct 10, 2019 4:57 pm, edited 1 time in total.
Re: How to test whether a variable is empty
Well, depending on what your doing, you just test to see if it is empty. This is as easy as an if/then one line statement, but you could go either way is or is not. I usually go with is not (for the symbolholics, <> ) :
it all amounts to the same thing. Here is an example ( I just ran it in the message box)
Code: Select all
if tmpVar is not empty then // do your stuff here since it has a value
# or
if tmpVar is empty then ....
#or
if tmpVar <> "" then ....
Code: Select all
put "" into tmpVar
if tmpVar is empty then answer true
-
- VIP Livecode Opensource Backer
- Posts: 9663
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: How to test whether a variable is empty
I am unsure what you are asking.
You cannot just ask if an "undefined" variable is empty. How would anyone, including LC, know what you meant? What if you had three undefined variables floating around?
And your snippet
is different in character, because now the variable "someVariable" is explicitly defined, if not yet examined. In other words, you did not discover a way to:
where "anUndefinedVariable" is, as you first mentioned, an undefined variable.
Or I am missing something...
Craig
You cannot just ask if an "undefined" variable is empty. How would anyone, including LC, know what you meant? What if you had three undefined variables floating around?
And your snippet
Code: Select all
"put someVariable + 0 into newVariable"
Code: Select all
put anUndefinedVariable + 0 into newVariable
Or I am missing something...
Craig
-
- VIP Livecode Opensource Backer
- Posts: 9663
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: How to test whether a variable is empty
Another take on your post. You wrote:
Again, this goes back to what I may be misunderstanding, that "undefined" variable thing. If I never define "temp" (which I can simply by using it) I cannot reference it. If I can reference it, I can do whatever I want with it.
Craig
But there is.There is no function "isEmpty()"
Code: Select all
on mouseUp
put "" into temp
answer isEmpty(temp)
put "X" into temp
answer isEmpty(temp)
end mouseUp
function isEmpty var
return (var = "")
end isEmpty
Craig
Re: How to test whether a variable is empty
What do you get fromMichaelBluejay wrote: ↑Tue Oct 08, 2019 7:01 pmBut this seems to work:
put someVariable + 0 into newVariable
If "newVariable is zero", then someVariable is empty.
Code: Select all
put 0 into someVariable
put someVariable + 0 into newVariable
Re: How to test whether a variable is empty
I must confess to being completely baffled by the concept of needing to know whether an undefined variable is empty in the context of being undefined versus "empty".
However, maybe the variableNames function is helpful to you:
However, maybe the variableNames function is helpful to you:
Code: Select all
if "undefinedVariable" is not in the variableNames then
answer "undefinedVariable is not defined, ergo contains no value, it is empty"
else
answer "undefinedVariable is in the variableNames so it IS actually defined, whether or not its contents are empty - no matter whether its name is truthful"
end if
-
- VIP Livecode Opensource Backer
- Posts: 9663
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: How to test whether a variable is empty
Either I am off in the weeds (likely) or others are.
So, Michael, do you understand what I am on about? A string, suddenly placed into use, is not a variable. At least not yet. It becomes one, however, if declared or if you put something into it. Then it is no longer a string. Your troubles at the start of this were due to the fact that you only had a string.
Craig
This is the beginning of this thread. To me, it implies a string, "undefinedVariable", not a variable. In other words, this "variable" has never been declared, nor used in any way, that is, populated on the fly.put undefinedVariable is empty -- returns false because it's evaluating the string "undefinedVariable"
The number of words in undefinedVariable? Nope, that's 1 ("undefinedVariable").
Put it in a field and check whether the field is empty? Nope, "put undefinedVariable in field output" results in the field containing the word "undefinedVariable".
So, Michael, do you understand what I am on about? A string, suddenly placed into use, is not a variable. At least not yet. It becomes one, however, if declared or if you put something into it. Then it is no longer a string. Your troubles at the start of this were due to the fact that you only had a string.
Craig
-
- Posts: 222
- Joined: Thu Jul 01, 2010 11:50 am
Re: How to test whether a variable is empty
Okay, I figured it out. A variable can test for empty only if it's been used already. So:
Dunbar, obviously I meant there is no NATIVE function isEmpty().
Code: Select all
on mouseup
put newVariable is empty -- returns false
-- It's empty b/c it has nothing in it, but LC evaluates the name of the variable as a string
end mouseup
on mouseup
put empty into newVariable
put newVariable is empty -- returns true
end mouseup
-
- VIP Livecode Opensource Backer
- Posts: 9837
- Joined: Sat Apr 08, 2006 7:05 am
- Location: Los Angeles
- Contact:
Re: How to test whether a variable is empty
You've made a useful discovery: In LiveCode, although quoting string literals is recommended it isn't required, at least when the string is a single word.MichaelBluejay wrote: ↑Tue Oct 08, 2019 11:11 pmOkay, I figured it out. A variable can test for empty only if it's been used already. So:
Code: Select all
on mouseup put newVariable is empty -- returns false -- It's empty b/c it has nothing in it, but LC evaluates the name of the variable as a string end mouseup on mouseup put empty into newVariable put newVariable is empty -- returns true end mouseup
So in the first example, "newVariable" is treated as a string literal, and it is not empty.
In addition to performing an operation on a variable to initialize it, you could also declare it:
Code: Select all
on mouseup
local newVariable
put newVariable is empty -- returns true
end mouseup
True, there is no function named "IsEmpty". Instead, LiveCode offers an operator, "is empty".Dunbar, obviously I meant there is no NATIVE function isEmpty().
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
-
- VIP Livecode Opensource Backer
- Posts: 9663
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: How to test whether a variable is empty
Yes, I know. Just as obviously, I was making a point.Dunbar, obviously I meant there is no NATIVE function isEmpty().
Glad you finally got the, er, point. And it is good practice to always quote literals. It removes a bit of ambiguity from ones coding; quoted literals are easy to spot, they distinguish themselves that way.
Craig