set (variable) to (definition)
Purpose
You use this construct in the definitions part of a
rule to declare a local variable.
Syntax
-
To define a variable as an object of a specific type:
set <variable> to <type> [in <list> | from <object>] [where <test>] ; -
To define a variable as multiple objects of a specific type:
set <variable> to <type> [in <list>] [where <test>] ; -
To define a variable as a constant:
set <variable> to <object> [where <test>] ; -
To define a variable as multiple constants:
set <variable> to <list> [where <test>] ;
Description
Variables produce more concise rules by replacing a value or the result of an expression with a short, convenient identifier. A local variable can be used anywhere within the rule in which it is defined, but is not available in other rules.
Enclose the name of each variable in single quotes. This is compulsory for variable names that contain spaces. While it is not essential to enclose one-word variable names in single quotes, it makes them easier to identify and reduces the risk of confusion.
Example
The following examples show how to define a variable as an object.
definitions
set i to an item;
set house to a house
where the price of this house is more than 1000;
The following examples show how to define a variable as a literal, string, or collection.
definitions
set category to Gold ;
set s to "a string";
set 'expensive items' to all items
in the items of the shopping cart of customer
where the price of each item is more than 200;
The following example shows how to define a variable as the result of an expression.
definitions
set expr to the price of the car of customer + 100;
set h2 to the house of customer
where the price of this house is more than 1000;
The following example shows how to define a variable as another variable.
definitions set expr2 to expr;