TEIMSI
Developer's platform of programming text codes
Home|Utilities|Forum|Documentation

Table of contents -> Chapter 9 - Functions for Arrays


The next topic is the description for the use of the following TEIMSI predefined functions:

1)- array(list)

2)- array_fill(amount,variable)

3)- count(array)

4)- explode(delimiter,string)

5)- getitem(array,integer)

6)- implode(delimiter,array)

7)- setitem(array,integer,variable)




array(list)

The "array(list)" function, returns an array composed by separated items by "," (comma), the items are passed as parameters. If it receives a nonnegative integer only, it indicates the size (number of items) of the array, then each item will take the value of the integer -1. The items in the list can be dynamic or static variables with strings, numbers (integer or double precision) and Boolean. Itís disabled the use of arrays as items of arrays for this TEIMSI version. To know the size of the array, the "count" function is used.

To write "var matriz=array()" is the same as to write "var matriz=array(0)", an empty array is created but it can be enlarged by assignment of some item which has an index greater or equal to the size of the array; see the example below.


Syntax
array(list)
 

Parameters
list/integer
List of dynamic or static variables delimited by ",". For example var thearray=array("Pino",true,2, "Eucalyptus",true,3, "Oak",true,1). It's possible to pass only one parameter, an integer that indicates the size of the array, which will be filled with integer type items of -1 value.
 

Returned value
array
TEIMSI variable with a data type of array.
 

Example:



	// The following sample program shows how to create an array, assign items, display size and its contents in a text whose lines are the items of it. The "implode" function is used which receives two parameters; the first is the separator (in this case is _nl equivalent to chr(13) + chr(10)) and the second an array, "implode" creates a string concatenating the array items and placing the separator between them.

	var myarray=array()

		// Assign channels to items 2 through 7. The 0 and 1 items stay with the default value (-1).

	for(var k=2;k<8;k++){
		myarray[k]="	The square root of "+k+ " is equal to " + sqrt(k)
	}
		// Now will extend the size of the array to 10, the item 8 will have the default value (-1).

	myarray[9]="<final item>"

	alert("The array has size "+count(myarray)+_nl+"The array has the following:"+_nl+implode(_nl,myarray))

	var myarray=array("Hello","World")

	alert("The array has size "+count(myarray)+_nl+"The array has the following:"+_nl+implode(_nl,myarray))



See also <<array_fill>>, <<implode>>


Go to top

array_fill

The "array_fill" function, returns an array which is composite by repeated items a certain amount of times (first parameter) using the original item (second parameter). That means that the first parameter is the size of the output array and it is an integer.


Syntax
array_fill(amount,variable)
 

Parameters
amount
An integer that indicates the amount of repeated items (that is the array's size).
 
variable
Variable used to copy each item into the array.
 

Returned value
array
TEIMSI variable with a data type of array.
 

Example:



	// The following sample program shows how to use array_fill.

	var array=array_fill(7,"Nice")
	alert("The array has a size of "+count(array)+_nl+"The content of the array is this:"+_nl+implode(_nl,array))



See also <<array(list)>>, <<implode>>


Go to top

count

The "count" function, returns an integer indicating the size of an array (that is the amount of items it has).


Syntax
count(array)
 

Parameters
array
TEIMSI variable with a data type of array.
 

Returned value
integer_number
TEIMSI variable with data type of a 32 bits integer number.
 

Example:



	var array=array(5,4,2,3)
	alert("The array has " + count(array) + " items")



See also <<array(list)>>


Go to top

explode

The "explode" function, creates an array of strings from a string (second parameter) which has array items separated by a separator string (first parameter).


Syntax
explode(delimiter,string)
 

Parameters
delimiter
TEIMSI variable with a data type of string.
 
string
TEIMSI variable with a data type of string.
 

Returned value
array
TEIMSI variable with a data type of array.
 

Example:



	var myarray=explode(",", "Hello,world,!")
	alert("The array has size "+count(myarray)+_nl+"The array has the following:"+_nl+implode(_nl,myarray))



See also <<implode>>, <<array(list)>>


Go to top

getitem

The "getitem" instruction returns an array's item. It's expressed with "thearray[integer]", where "thearray" is the first parameter and "integer" is the second one.


Example:



	var myarray=explode(",", "Hello,world,!")

	alert(myarray[1])

	alert(getitem(myarray,1))		//	Performs the same task.


See also <<setitem>>


Go to top

implode

The "implode" function, returns a string which is the result of joining the strings of an array (second parameter) using a given separator (string first parameter).


Syntax
implode(delimiter,array)
 

Parameters
delimiter
TEIMSI variable with a data type of string.
 
array
TEIMSI variable with a data type of array.
 

Returned value
string
TEIMSI variable with a data type of string.
 

Example:



	var myarray=array("There are ",4," cardinal points.")

	alert("Content of the array:"  + implode("", myarray))



See also <<array(list)>>, <<explode>>


Go to top

setitem

The "setitem" instruction sets an item of an array. It's expressed with "thearray[integer]=variable".


Example:



	//	The following program shows how to set an item in an array.

	var thearray=explode(",", "hello,world,!")

	alert("The array has size "+count(thearray)+_nl+"The array has the following:"+_nl+implode(_nl,thearray))

	thearray[1]="Comrade"		//	Same as: setitem(myarray,1,"Comrade")
	thearray[3]="."

	alert("The array now has size "+count(thearray)+_nl+"The array has the following:"+_nl+implode(_nl,thearray))


See also <<getitem>>


Go to top