Array operators

Arithmetical operators

An item, or a group of items, may be added or removed with the + and - operators.
The + sign has the same effect than the push() method when there is a single one element. It has the same effect than the merge() method when this is an array.
If an array has to be added as an element (thus a two-dimensional array has to be created), the array to insert must be assigned a dyn before.
The - sign has not direct equivalent, the same effect may be obtained by the pop(int) method with a position as parameter.

Examples of addition followed by a substraction of arrays. array a = array("one", "two")
array b

b = a + array("three", "four")
b.display()

a = b - array("one", "four")
a.display()
Displays successively: array(
 0 : one
 1 : two
 2 : three
 3 : four
)

array(
 0 : two
 1 : three
)



Only the + and - arithmetical operators are usable on arrays.

Logical operators

You may use for dynamic lists (array or dict) the binary operators:

&
|
^
intersection
union
complement of intersection


The intersection of two lists returns only elements that are parts of both the two lists.

Examples of intersection array a = array(1,2,3,4) & array( 3,4,5,6)
a.display()
Displays: array(
 0 : 3
 1 : 4
)


The union of two lists is a new list made of the elements of the first one, plus those of the second one but if they are already in the first one.

Examples of union: array a = array(1,2,3,4) | array( 3,4,5,6)
a.display()
Displays: array(
 0 : 1
 1 : 2
 2 : 3
 3 : 4
 4 : 5
 5 : 6
)



With these powerful operators in the pocket, you can easily perform such big operation as testing if a list if a part of another list, with just one statement!

a and b are two arrays.
This statement returns true if a is a part of b:
if (a & b) = a : ... do something...
An example in the "fcat.sol" script.

The "in" operator

This special operator may be used to test if a value is contained inside a list (array, dict or even a text), and to scan the content too.

The syntax to test the appurtenance is:
if variable in array

Examples of "in" array a = array(1,2,3,4)
if x in a print "inside"
if x in array(1,2,3) print "inside"


To browse the array, the syntax is:
for variable in array

Examples of loop: for text t in array("one", "two", "three")
  print t
/for
Displays: > one
> two
> thre

 

The "nil" operator

The y object must be searched out of the a array.
dyn x = a.find(y)
If y is not found, the value of x will be "nil".
Nil comes from the Lisp language, and means for "not in list".

To remove an element at the k position, type:
a[k] = nil

Take note that to insert the v value at the n indice, a method has to be used:
a.insert(n, v).


 Exercises

 

1) From this array:
a = array(1,2,3,4,5,6)
Remove all odd elements.
Use a loop with this header:
for int i in a.size() - 1 .. 0 step -1

Answer