The defined() function returns TRUE if a variable has a value.
This example is a refinement of the if example. The section “if else statement” on page 77 provides additional information.
By using the defined() function, several lines of the script are removed, including a foreach statement and an if statement.
The single if statement verifies whether a table key entry exists in the table. If the name exists, the number of animals is added to the animal count already stored in the table. If the name does not exist, the animal name and number are added to the table.
ASL Script (defined_do.asl): z = table(); START { rep(IMPORT) } IMPORT { animal:word count:integer eol } do { if (defined(z[animal])) { z[animal]=z[animal]+count; } else { z[animal]=count; } } EOF do { foreach i (z) { print(i." count ".z[i]); } } Input (defined_do.txt): dog 3 cat 4 canary 3 cat 2 dog 5 dog 1 cat 1 Output: $ sm_adapter --file=defined_do.txt defined_do.asl dog count 9 cat count 7 canary count 3 $