How to test whether a variable is empty

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
MichaelBluejay
Posts: 222
Joined: Thu Jul 01, 2010 11:50 am

How to test whether a variable is empty

Post by MichaelBluejay » Tue Oct 08, 2019 7:01 pm

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...).
Last edited by MichaelBluejay on Thu Oct 10, 2019 4:57 pm, edited 1 time in total.

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: How to test whether a variable is empty

Post by bogs » Tue Oct 08, 2019 8:00 pm

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
Image

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9645
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: How to test whether a variable is empty

Post by dunbarx » Tue Oct 08, 2019 8:38 pm

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9645
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: How to test whether a variable is empty

Post by dunbarx » Tue Oct 08, 2019 8:45 pm

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

SparkOut
Posts: 2852
Joined: Sun Sep 23, 2007 4:58 pm

Re: How to test whether a variable is empty

Post by SparkOut » Tue Oct 08, 2019 10:04 pm

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
?

SparkOut
Posts: 2852
Joined: Sun Sep 23, 2007 4:58 pm

Re: How to test whether a variable is empty

Post by SparkOut » Tue Oct 08, 2019 10:14 pm

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9645
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: How to test whether a variable is empty

Post by dunbarx » Tue Oct 08, 2019 10:20 pm

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

MichaelBluejay
Posts: 222
Joined: Thu Jul 01, 2010 11:50 am

Re: How to test whether a variable is empty

Post by MichaelBluejay » 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
Dunbar, obviously I meant there is no NATIVE function isEmpty().

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9823
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: How to test whether a variable is empty

Post by FourthWorld » Wed Oct 09, 2019 12:04 am

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".
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9645
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: How to test whether a variable is empty

Post by dunbarx » Wed Oct 09, 2019 12:44 am

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

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”