The Number 1 independent website for ACL information!

TexasACL User Group
About Us
Training
Why ACL
Essays & Scripts
ACL News
Events
Links
FAQ
Site Map
Support Us
Variable Substitution

After developing the basics of script writing, the next step is to learn the basics of variable substitution. 

For example, suppose that you have written the simple script:

OPEN Tablename
EXTRACT FIELDS ALL TO TEMP1 IF date_field > `20090101` AND amt_field > 10000

The above script will open the able and identify records that are more recent than January 1, 2009 and had an greater than 10,000.  This is a nice basic script, but suppose you wanted to allow a user to select a different date and a different amount.  There are several different ways to ask a user for input, the most basic way is via an ACCEPT command.  The ACCEPT command, will allow the programmer to ask for one piece of information and has the format:

ACCEPT "message_text" <FIELDS "list_item"> TO variable_name

So, in order to ask for user input with the date and amount, you would need to modify the script as follows:

OPEN Tablename
ACCEPT "Please enter the date in YYYYMMDD format" TO v_date
ACCEPT "Please enter the new threshold amount:" TO v_amt

Please note how the variables, used in the example start with the prefix "v_", this is part of the best practices covered in Naming Conventions.  Using the two accept commands, you were able to define two variables v_date and v_amt.  In order to use them in the EXTRACT statement, you simply can replace the hard coded values with the variables.  There are two ways to do this, the first involves using %% surrounding the variables and the second does not. 

By surrounding the variable with the percentage signs, you are telling ACL to replace the variable with the value of the variable.  As a general rule, you do not have to use the percentage signs when using variables that represent dates, numerics, or logical parameters.  When dealing with these values, the principle difference in using the percentage sign centers on whether or not the value is replaced in the log.  If you encase a variable in percentage signs, then it will appear in the log as the value of the variable.  On the other hand, if you do not use the percentage signs, then the variable (v_amt) will appear in the log.

When using character variables, you must use the percentage marks to surround the variable.  In other words, suppose that you had a variable v_name that was defined as "Porter Broyles".  In order to use the variable v_name, it would have to be encased in percentage marks as "%v_name%".  Character variables can represent fieldnames, other variables, or actual character based values.   As a general rule, if the variable represents a fieldname or another variable, it does not require quotation marks.  If the variable represents actual ASCII characters, it will have to be encased in quotation marks.  This will be delved into in more detail in Variable Substitution Part 3.

To finish the code described above:

OPEN Tablename
ACCEPT "Please enter the date in YYYYMMDD format" TO v_date
ACCEPT "Please enter the new threshold amount:" TO v_amt
EXTRACT FIELDS ALL TO TEMP1 IF date_field > v_date AND amt_field > v_amt

(Or EXTRACT FIELDS ALL TO TEMP1 IF date_field > %v_date% AND amt_field > %v_amt%)