The scan structure
scan .. by is a powerful control structure invented in Scriptol and
it can replaces, by two or three lines, a page of code required in an old
language as C++ or Pascal.
Syntax:
scan array [, array]* (by function | block of statements /scan)
That means:
Scan the array element by element, and use each one as parameter of a function
or a block of statements.
Using a function
scan a, b, ... by function
a, b, etc... are arrays and function is the name of a user-defined function.
One by one, elements of the array a and b together are given as parameters
to a function and processed by this function.
When the last element of one of the arrays has been reached, the process is
terminated.
Using a block of statements
scan a, b, ...
... instructions...
/scan
Here, elements of the arrays are not processed by a function, but by the enclosed
block of statements instead. .The by keyword is not used in this case.
Example of scan by a bloc of statements. The next element from a is concatened with the next one from b, and the result is pushed into the c array. |
array
a = array(1,2,3,4,5,6) array b = array("a","b","c","d") array c = array() scan a,b c.push(b[] + str(a[])) /scan c.display() |
Displays: | >
array ( "a1", "b2", "c3", "d4" ) |
The empty index [] designates the currently pointed out element in the
list. We can also use it in assignement, using the previously defined
array:
Using current index in a compound assignment. | array
a = array(1,2,3,4,5,6) scan a a[] * 10 `next value multiplied by 10 /scan a.display() |
Displays: | >
array ( "10", "20", "30", "40", "50", "60" ) |
Two-dimensional array
A two-dimensional array is created, by defining a one-dimension array the
element of which are also arrays.
Here is an example to create and scan a two-dimensional array.
Creating and browsing a two-dimensional array. | array
a2 = array( ("a", "b", "c"), ("x", "y", "z"), (1, 2, 3) ) scan a2 // converting dyn into arrays array x = a2[].toArray() scan x print x[] /scan /scan |
It would be simpler to avoid creating the x temporary array, as in
the following example (using the one-statement syntax):
scan aa
scan aa[] print aa[][]
/scan
This works with Scriptol-C++, but the Php 4 interpreter doesn't allow
this and thus this is not used in Scriptol to keep compatibility.
Exercises |
|