Page 1 of 1

How to test whether a variable is empty

Posted: Tue Oct 08, 2019 7:01 pm
by MichaelBluejay
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...).

Re: How to test whether a variable is empty

Posted: Tue Oct 08, 2019 8:00 pm
by bogs
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, <> ) :

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 ....
it all amounts to the same thing. Here is an example ( I just ran it in the message box)

Code: Select all

put "" into tmpVar

if tmpVar is empty then answer true

Re: How to test whether a variable is empty

Posted: Tue Oct 08, 2019 8:38 pm
by dunbarx
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

Code: Select all

 "put someVariable + 0 into newVariable" 
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:

Code: Select all

put anUndefinedVariable + 0 into newVariable
where "anUndefinedVariable" is, as you first mentioned, an undefined variable.

Or I am missing something...

Craig

Re: How to test whether a variable is empty

Posted: Tue Oct 08, 2019 8:45 pm
by dunbarx
Another take on your post. You wrote:
There is no function "isEmpty()"
But there is.

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
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

Re: How to test whether a variable is empty

Posted: Tue Oct 08, 2019 10:04 pm
by SparkOut
MichaelBluejay wrote:
Tue Oct 08, 2019 7:01 pm
But this seems to work:

put someVariable + 0 into newVariable

If "newVariable is zero", then someVariable is empty.
What do you get from

Code: Select all

put 0 into someVariable
put someVariable + 0 into newVariable
?

Re: How to test whether a variable is empty

Posted: Tue Oct 08, 2019 10:14 pm
by SparkOut
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:

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

Re: How to test whether a variable is empty

Posted: Tue Oct 08, 2019 10:20 pm
by dunbarx
Either I am off in the weeds (likely) or others are.
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".
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.

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

Re: How to test whether a variable is empty

Posted: Tue Oct 08, 2019 11:11 pm
by MichaelBluejay
Okay, 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
Dunbar, obviously I meant there is no NATIVE function isEmpty().

Re: How to test whether a variable is empty

Posted: Wed Oct 09, 2019 12:04 am
by FourthWorld
MichaelBluejay wrote:
Tue Oct 08, 2019 11:11 pm
Okay, 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
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.

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
Dunbar, obviously I meant there is no NATIVE function isEmpty().
True, there is no function named "IsEmpty". Instead, LiveCode offers an operator, "is empty".

Re: How to test whether a variable is empty

Posted: Wed Oct 09, 2019 12:44 am
by dunbarx
Dunbar, obviously I meant there is no NATIVE function isEmpty().
Yes, I know. Just as obviously, I was making a point.

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