How do you reset a variable to empty?

Hello,
I’m trying to clear a variable and I was wondering if it is it better to clear a variable using brackets or quotes?

set the_variable to “”

or

set the_variable to {}

Tx

Keith

It depends on what you intend to place into the variable, and how you’re using it. Typically, a rule of programming in general is to define variables as one class and then continue to use only that class in any future references to it. Because applescript does not have separate naming syntax to distinguish between potential classes or values of a variable… such as strings, integers, arrays (lists), etc… it is up to you as a programmer to create a method of differentiating them in your code or keeping track of what kind of value they hold. Although you may sometimes be able to get away with setting a variable’s value across class lines, you may run into problems with this, so it is best to simply set a value based on what type of value the variable represents. For example, you have three variables which hold different types of values…

property anInteger : 5
property aString : "hello"
property aList : {"how","are","you"}

To reset or “zero” their values, it would be best to reassign them “null” values in line with what kind of values you intend them to contain when they are NOT zeroed. By doing this, you better control your code flow, and reduce the potential for errors caused by class conflicts. If your code were to evaluate, say, a list variable… which contained an empty string, rather than an empty list… your code may break and throw an error. I would recommend resetting your variables as such…

set anInteger to 0
set aString to ""
set aList to {}

Hope that helps…
j

In practice, I use the methods that jobu demonstrated. However, it may be worth point that “missing value” is the AppleScript equivalent to the “null” or “nil” of other languages.


set aVariable to missing value