Array

  The first artificial intelligence language, Lisp, was entirely based on dynamic lists. This denotes how useful are lists in programming meanwhile they have been not implemented into classical popular languages.
All lists functions are implemented into Scriptol as associatives data structures and two types of dynamic lists of objects are distinguished:
- array: keys are integer numbers.
- dict: keys are texts.

We will start to study the power of dynamic lists with arrays.

What is an array?

A statement as: a[1000]="x" adds the "x" element with the indice 1000...
But this is not the way an array must be filled because indices will be renumbered at the next operation on the array!
Example to demonstrate how the are filled...
The a array is declared and assigned with these statements:
array a
a.push("one")
a.push("two")
a.push("last one")
Type a.display() to view the content, if the program is compiled to native, this output is displayed:
array( 0 : one 1 : two 2 : last one )
The interpreter has a different output:
array ( [0] => one [1] => two [2] => last one )

Declaration

As of any Scriptol object, an array is declared by the type followed by an identifier:
array a

The constructor of the array has the form:
array(value, value, etc...)
An empty initializer is written:
array()

Thus the complete declaration syntax is:

array identifier [ = array( [element [, element]* ) ]

That means for:
- array type,
- name of the object,
- optionally, constructor,
- symbol =
- array type,
- between parenthesis, from 0 to n elements.


The constructor may be used directly into an expression, as a literal:
a = array(1, 2) + array(3, 4)

Stack operations

An array may be used as a stack, new elements are pushed at start or at end of the array; and poped in the same manner...
Stack methods are these:
a.push("item")       ...add an element to the end of the list
a.unshift("item")    ...add an element to the start of the list
a.pop()                   ...read and remove the last element.
a.shift()                   ...read and remove the first element.

So, you can read and eleminate entries of an array by successives statements as:
print a.shift()

Assignment

The following example show haw an array must bu filled... It is declared first and push statement add elements one after another...

Example showing using an array as a stack. array a
a.push("one")
a.push("two")
a.push("last")

a.display()     `displaying the content
Displays in Scriptol Php: array(
  [0] => one
  [1] => two
  [2] => last
)
Displays in Scriptol C++: array(
 0 : one
 1 : two
 2 : last
)

 

Direct assignment

It is possible to assign an element directly at a given position...

Example of direct assignement: a[1000] = "x"
a.display()
Displays in Scriptol C++: array(
0 : one
1 : two
2 : last
1000 : x
)


However the 1000 indice is just temporary, don't expect it kept as is, after any statement has changed the array. A such statement with the form a[n] = x is convenient to changes values in an array, but you must not create an array this way, this is the way a dictionary (dict) must be filled, as you will see further.


Arrays allow to scan their content by incrementing a numerical key that is also the index of the position. Dictionaries with textual keys allow to retriive an element by the key, the location of which having no importance.

You can use an array as a stack, or perform list processing as with Lisp or use array as sets thanks to intersection and union operators, or apply methods to get the maximum, the minimum or the sum of elements.


 Exercises
1) Given the following arrays:
array a = ("ax", "ay", "az")
array b = ("b1", "b2", "b3")
Remove successively the element of the "a" array and add them to the "b" array (the number of statements is the number of elements).
Display the result.

Answer