Example: Struct Basics
This ProcScript example demonstrates how multiple references access the same Struct,
and that a Struct is deleted when there is no longer a struct
variable that refers
to it or any of its parents.
entry STRUCT_BASICS variables struct vStruct, vPerson ; declare struct variables endvariables ; --- Build up a Struct --- ; Create a Struct vStruct = $newstruct; Create Struct members:
vStruct->group = $newstruct vStruct->group->person = $newstruct vStruct->group->person->firstname = "John" vStruct->group->person->email = "john@home.com" ; Display the Struct in the message frame: call printHeader("STRUCT_BASICS") ; display entry header in the message frame putmess "Struct referred to by vStruct: " putmess vStruct->$dbgstring
; --- Update the Struct using a different variable --- vPerson = vStruct->group->person
putmess "Struct referred to by vPerson: " putmess vPerson->$dbgstring
vPerson->email = "john.smith@home.com"
putmess "Struct referred to by vStruct: " putmess vStruct->$dbgstring
putmess "Note: 'email' changed with vPerson is also changed in vStruct: " ; --- Delete a Struct --- ; Display the parent of vPerson
putmess "The parent of vPerson is: [%%(vPerson->$parent->$name)%%%]" ; Assign an empty Struct to vStruct, and display the Structs vStruct = $newstruct
putmess "After assigning a new value to vStruct, vStruct points to empty Struct:" putmess vStruct->$dbgstring putmess "vPerson is still valid: [%%(vPerson->firstname)%%%]" putmess "but the parent of vPerson is now invalid: [%%(vPerson->$parent->$name)%%%]" putmess "resulting in a ProcScript error: %%$procerror: %%$item("DESCRIPTION", %\ $procerrorcontext)" end ; entry STRUCT_BASICS
The code builds up the Struct, updates the Struct using a different struct variable, then deletes a Struct.
- Create a new empty Struct and assigns it to
the
struct
variable. However, this is not strictly required. The Struct is created on the fly when a member is assigned to the variable. - Create the members of a Struct. This must be done explicity; members are not created on the fly.
- Display a representation of the Struct in
the message frame using the $dbgString (or $dbgstringplain)
Struct function.
Struct referred to by vStruct: [] [group] [person] [firstname] = "John" [email] = "john@home.com"
- Get a reference to the
person
node and assign it to the struct variablevPerson
. - Display a representation of the
person
Struct in the message frame:Struct referred to by vPerson: [person] [firstname] = "John" [email] = "john@home.com"
- Change the value of the
email
member of theperson
node refered to byvPerson
. - Display a representation of the main Struct
(
vStruct
) in the message frame:Member 'email' changed with vPerson is also changed in vStruct: [] [group] [person] [firstname] = "John" [email] = "john.smith@home.com"
- Show that the parent Struct of the
person
member can be accessed usingvPerson
.Note: When using the -> operator in string substitutions, the expression must be enclosed within parentheses.The parent of vPerson is: [group]
- Assign a new member to the
vStruct
variable. This deletes the Struct that it referred to before. Theperson
Struct remains but has lost its outer context (its direct parent and above), so an attempt to get its parent results in an error. After assigning a new value to vStruct:vStruct points to empty Struct:
[] = ""
but the parent of vPerson is now invalid:[]
resulting in a ProcScript error:-84: Not a valid Handle or Struct specified