[ ]
| Syntax |
[expression] or, identifier [expression]
|
| Action |
These two forms of the bracket operator have very different meanings. In the first form above, expression is evaluated as a string, and the result is then treated as an identifier. Identifiers are the names of objects in the database and in scripts, which normally must consist strictly of alphanumeric characters (and possibly underscores). The bracket operator allows strings that contain spaces or other special characters to be used as identifiers. It also allows a simple identifier to be replaced by a calculation of any kind. The second form above is referred to as an array operation. In this case, identifier must specify a table, list, record, string or binary value, and expression specifies an item in that value. For tables and records, the item may be specified either by index (a number) or by name (a string). For other value types, the item must be specified by index. The first item in an array has an index of one.
|
| Examples |
edit (@examples. ["Sample Outline 1"]) » true
Sample Outline 1 contains spaces, and would normally be interpreted as two identifiers followed by a number - a syntax error, since it's not a valid expression. So we enclose the name in quotes, which groups the entire sequence of characters into a single
This example demonstrates how the bracket operator allow you to substitute an expression for an identifier. First, the expression user.initials is evaluated. The resulting value is used to name an item in the "people" table, the one belonging to the
At first this can seem puzzling; there is a table named verbs in the system table. However, that's not what this expression specifies. The string "system.verbs", enclosed in the brackets, is interpreted as a single identifier, so the expression specifies
Here, we're using brackets as an array operation; note that there is no dot between examples and the first bracket. The simple expression "5" indicates the fifth item in the table. The nameOf operator returns the name of that item.
x = "abcdef"; return (x [6])
x = binary ("abcdef"); return (x [1])
These examples show how you can use brackets to access an item in a list, a string, and a binary value respectively, treating the value as an array. The type of the item value depends on the type of the array value; an item in a string is a character, |
| Notes |
When expression is a string and the array value is a table, there is no functional difference between using the string in an array operation and using it as a bracketed identifier in a dotted id. That is: identifier [expression] identifier.[expression] both specify the table item whose name matches the result of expression. In addition to the requirement that identifiers contain only alphanumeric characters and underscores, they must not begin with a number, and cannot be the same as a reserved word of the UserTalk language. So if you need to create or refer to an object named while, the bracket operator must be used as shown in the first example above to prevent the name from being mistaken as a looping statement.
|
| See Also |
. "{ }"
|