Bracket syntax for dynamic access

Wherever dynamic access is valid, you can reference a field by using a string variable, constant, or literal in brackets. Each content-filled pair of brackets is equivalent to a dot followed by a valid identifier.

Although any keys specified in a dictionary declaration must fulfill the rules for EGL identifiers, you can specify a wider range of keys by using bracket syntax in EGL assignment statements. Bracket syntax is required in the next example, where two entries are added to a dictionary and the value in each of those entries is retrieved:
  row Dictionary { lastname = "Smith" };
  category, motto STRING;

  row["Record"] ="Reserved word";
	 row["ibm.com"]="Think!";	

		category = row["Record"];
  motto    = row["ibm.com"]
If you reference a value by using an identifier in dotted syntax, you can reference the same value in bracket syntax by using a string that is equivalent to the identifier. The following assignments have the same effect:
  row.age = 20;
  row["age"] = 20;

Assume that you declared a record named myRecordVar01, which includes a field named myRecordVar02, and that myRecordVar02 is itself a record that includes the previous dictionary. A valid reference is as follows:

  myRecordVar01.myRecordVar02.row.lastName
Access is static for most of that reference. Dynamic access begins when you access the field in the dictionary. Assume that these constants are in scope, however:
  const SECOND STRING = "myRecordVar02";
  const GROUP  STRING = "row";
  const LAST   STRING = "lastName";
You can code the previous reference as follows:
  myRecordVar01[SECOND][GROUP][LAST]

The first symbol in a reference must always be a valid identifier, but in this case, dynamic access is in effect after that identifier.

You can mix the dotted and bracket syntaxes. For example, the following reference is equivalent to the previous one:
   myRecordVar01[SECOND].row[LAST]
As a final example, consider a reference with an array index:
  myRecordVar01.myRecordVar02.myRecordVar03[3][2].myInt
Assume that these constants are in scope:
  const SECOND  STRING = "myRecordVar02";
  const THIRD   STRING = "myRecordVar03";
  const CONTENT STRING = "myInt";
You can code the previous reference in these ways:
   myRecordVar01[SECOND][THIRD][3][2][CONTENT]

   myRecordVar01[SECOND][THIRD][3][2].myInt

   myRecordVar01.myRecordVar02.THIRD[3][2][CONTENT]

Related concepts
Abbreviated syntax for referencing fixed structures
Dynamic and static access
Function part
Parts
Program part
References to parts
References to variables in EGL
Scoping rules and "this" in EGL
Fixed structure
Typedef

Related tasks
Declaring variables and constants in EGL

Related reference
Arrays
Function invocations
Function part in EGL source format
Options records for MQ records
Primitive types
Use declaration

Feedback
(C) Copyright IBM Corporation 2000, 2005. All Rights Reserved.