Livecode Wiki
Advertisement

Mostl computer languages use variables to store temporary data. But LiveCode is a little different in how variables are used. LiveCode uses 4 different styles of variables, they differ in how long they exist. Variables that exist for much of the program run the risk of being inadvertently changed in value by the programmer. Such problems are difficult to find. If a variable is limited in existence, that problem is lessened.

The length that a variable exists is called the scope of a variable. It can be local or global. A local variable has an existence to a small part of the program. A global variable has an existence to the full program. Livecode even has a variable who's value is stored with the program and is available when the program is used next time.

Livecode variables are called containers, they can store more that one value. They resemble lists in the way they operate.

A put command is used with variables. If the variable has not been named before, a local variable will be created by that name. If the variable already exists, the value is changed by the put command. For example:

put 23 into myVari                      -- myVari = 23  (integer)
put "I live in Phoenix" into myVari     -- myVari = "I live in Phoenix"  (string)
put false into myVari                   -- myVari = false  (boolean)

For the put command, the value can instead be an expression which evaluates to a value. You can use the & character to concatenate strings. For example:

put 17 - 5 into myVar                         -- myVar = 12
put myVar + 54 into myAnswer                  -- myAnswer = 76
put "Hello" &  " world" into myVar            -- myVar = "Hello world"

It is not possible to hit a return or tab key when typing in a put command. But if you type in those words, Livecode will put in those actions when the put command is executed. See the next example:

put "Hello world" & return into myVar   -- myVar is given a first line with a return
put TAB & "How are you?" after myVar    -- myVar gains a second line with a tab
put myVar into myBusiness               -- myBusiness = "Hello world'
                                        --     "How are you?"

Be sure to check out the operation of the Put command.

The scope of a local variable is limited to the message handler where the variable resides. Suppose a variable with the same name is used in two different handlers. After the first handler is executed, the variable ceases to exist. So when the second handler is executed, the variable does not have a value from the first handler. It is a totally separate variable from the first handler.

The scope of such a variable is local, meaning local to the handler in which the variable is used. It is not necessary to declare a variable as a local variable, it is assumed. For example:

on mouseUp                   -- myVar doesn't yet exist
  answer myVar               -- answer is empty, myVar doesn't yet have a value
  put "Hello" into myVar     -- myVar = "Hello"
  answer myVar               -- answer = "Hello"
end mouseUp                  -- with this statement, myVar no longer exists
on mousedoubleUp             -- myVar doesn't yet exist
  answer myVar               -- answer is empty, myVar doesn't yet have a value
end mousedoubleUp            -- with this statement, myVar no longer exists

Sometimes, its important to be able to give a variable a value in one handler and use it in another handler. There are two easy possibilities to use and later a better way. Suppose a variable is to be used in two handlers in the same script, for example the script of a button. If the local statement is used at the top of the script outside of any handler, then any variable so declared can be used in two or more handlers of that script is the same. The scope of such a variable is a local script, meaning local to the script in which the variable is used. it is not necessary to declare a variable as a local variable in each handler, it is assumed. For example:

local tmpAppPath

on preOpenStack                  -- tmpAppPath exists with scope local script
  // get the application stack folder
  put the effective filename of this stack into tmpAppPath  -- variable has value
end preOpenStack                 -- with this statement, variable still exists

on mouseUp                       -- tmpAppPath still exists with value
 // display the path
 answer tmpAppPath               -- answer = value
end mouseUp                      -- with this statement, tmpAppPath no longer exists

Sometimes, its important to be able to give a variable a value in one handler and somewhere in another part of the program use it in another handler. There are two further possibilities one better than the other. If the global statement is used at the top of the first script outside of any handler, then any such declared variable used in the handlers of that program are the same. The scope of such a variable is global, meaning global to the program in which the variable is used. it is necessary to declare a variable as a global variable in each handler it is used. If not declared as global, a variable will be considered a different local variable. An example of a global declaration:

global Circuit                       -- Declaration of global variable
on mouseUp                           -- Start of handler
   global Circuit                    -- Necessary to use global variable in handler
   put 45 into Circuit               -- Circuit = 45
   put Circuit into Rex              -- Rex = 45
end mouseUp                          -- End of handler

The last possibility is to use Custom properties. Every object in Livecode has a set of built-in properties. To those, the programmer can add his own set of custom properties. The values for those properties can be accessed from anywhere in the program. Their scope is global to the program. When the program is saved, the values for the custom properties are saved also. When the program is started up again, those values are the same as the previous session. For example we create an appFolder property:

on preOpenStack 
  // get the folder where the application stack is...
  put the effective filename of this stack into tmpAppPath 
  set the appFolder of me to tmpAppPath
end preOpenStack

on mouseUp
 // display the path in an answer dialog...
 answer the appFolder of me
end mouseUp

A variable can have more than one value. A variable will inaturally operate as a list of items. Use chunk expression to access the values in the list. For example:

put "Red, Pink, Scarlet" into myColors 
put "Yellow, Sunshine, Blonde" before myColors 
put "Purple, Violet" after myColors
       -- myColors = "Yellow, Sunshine, Blonde, Red, Pink, Scarlet, Purple, Violet"
put number of items of myColors into myItems          -- myItems = 8
put third item of myColors into myIdeal               -- myIdeal = "Blonde"
put character 1 of fourth item of myColors into myN   -- myN = "R"

A list variable can be changed into an array using the split command. The array name will be the same as the previous list variable. Now each item in the array can be accessed using array notation instead of chunk expressions. For example:

put "Yellow, Sunshine, Blonde, Red" into myColors
split myColors by comma                            -- myColors[1] = "Yellow"
                                                   -- myColors[2] = "Sunshine"
                                                   -- myColors[3] = "Blonde"
                                                   -- myColors[4] = "Red"

The above operation can be reversed by using the combine command. The array will be put back as a list variable with the same name.

A variable can become a list of lists. One way to do this:

put "Red, Pink, Scarlet" & return into myColors     
put "Yellow, Sunshine, Blonde" & return after myColors  
put "Purple, Violet" & return after myColors
put myColors into goodColors              -- goodColors = "Red, Pink, Scarlet"
                                          --              "Yellow, Sunshine, Blonde"
                                          --              "Purple, Violet"

See also: Getopt, Special_chars_like_+,-,&,...

Advertisement