Linguist Collections
A Collection is like a structure in other languages. The choice of a different term is not simply perversity, but to remind users that the terms are not synonymous and care should be taken.
A Collection can contain any number of items of different kinds. These will normally be variables or buffers, but there's no reason why other kinds of variable can't be included too. And most importantly you can include other collections. Let's start with a data record for a person:
collection person is buffer Name and variable Age and buffer Address 4 buffer ZipCode
and so on. The Address item here is an array of buffers to hold up to four lines of an address. We can instantiate variables of this type as follows:
person Fred person Alice
To set the name of Fred, use the command put "Fred Bloggs" into Fred.Name. To access the second line of the address of Alice, use
index Alice.Address to 1 put Alice.Address into ...
Note the use of the dot notation to access the fields of the person collection. (If you don't understand the use of index, go look it up now.)
Next we have a collection that holds data about an employee in a company:
collection employee is person PersonalDetails and variable EmployeeNumber and variable Salary
etc. Notice that we've embedded the person collection inside this one. Next we declare an array of variables using this type:
employee MyStaff 100
which leaves room for up to 100 employees. So to set the salary of the 34th employee:
index MyStaff to 34 put 35000 into MyStaff.Salary
If we want to access the address of an employee, we must also index the Address field:
index MyStaff.PersonalDetails.Address to 2 put MyStaff.PersonalDetails.Address into ..
Since we've already set the index for MyStaff, the system is able to determine which group of four buffers is being referred to. So it's essential to start at the outest level and work inwards.
This can get rather clumsy, so you can shortcircuit the process a little using an alias. First create a 'spare' person:
person ThisPerson
then alias ThisPerson to the required element of MyStaff:
index MyStaff to 34 alias ThisPerson to MyStaff.PersonalDetails
Now we can access the address record:
index ThisPerson.Address to 2 put ThisPerson.Address into ..
This is still a little clumsy, but scripting languages were never designed for heavy data management. The advantage of being able to group data using collections probably outweighs the clunkiness of access. In case you're interested, although the long list of names separated by dots makes for a lot of typing, unlike other languages there's little extra run-time overhead. In this case there's a single buffer created for all the Address fields in MyStaff, containing 400 separate elements. The compiler generates code to directly access the buffer, which then computes which element to go to based on its parents' element counts and indices.