Recital Arrays

From Recital Documentation Wiki
Revision as of 14:47, 23 July 2010 by Yvonnemilne (Talk | contribs)

Jump to: navigation, search

Recital Arrays

An Overview of Arrays in Recital

How to Create a Static Array in Recital

Static Arrays

A static array is an ordered list of elements (variables) that is of a fixed size (number of elements). You declare a static array by specifying the number of elements when you declare a variable.

private tab[ 20 ]    // declare a static array of 20 elements all initialized to False
 
// iterate through the array (note the use of the alen( ) function to find the length of the array
for i=1 to alen( tab )
    // change each array element to hold a numeric value
    tab[ i ] = i
endfor

You can initialize a static array with one statement.

// declare the array and init all elements to false
declare tab[10, 10]
 
// init all elements to zero
tab = 0

You can create and initialize static arrays using static array initializers.

// simple one dimensional array with 2 elements
private tab = { "Hello", "world" }
 
// two-dimensional array of two rows with three columns in each row
private tab2 = { { "Hello", 10, date() ], { "world", 20, date()+1 } }
 
// create an array on the fly
mytab = { 10, 20, 30, 40, 50, 60 }

You can view the contents of a static array using the echo or ? commands.

? tab

How to Create an Associative Array in Recital

An associative array (also known as a dynamic array) is a collection of key/value pairs where the key can be used to retrieve the value. Associative arrays are dynamic, meaning that elements can be added and removed dynamically.

private tab[]    // note the use of [] to denote a dynamic array
 
tab["name"] = "bill"
tab["age"] = 25
tab["dob"] = date()

Associative arrays can be created and initialized in one statement using the array( ) function.

tab = array("name" => "bill", "age" => 25, ""dob" => date())

You can view the contents of an associative array using the echo or ? commands.

? tab

Accessing Elements in a Static Array

Accessing Elements in an Associative Array

Changing, Adding and Removing Elements From Arrays

  • adel() - delete an element from an array
logical = adel(arrayname as character, element as numeric | row as numeric [, dimension as numeric])

Looping Through Arrays

The foreach...endfor command is used to iterate over each element in an array or member in an object or associative array.

foreach <array> as <value as variable> | <array> as <key variable> => <value as variable>
<statements>
endfor

The array must be the name of an existing array or object. The value is a reference to be loaded with the contents of each element or member in turn. Using the <key> => <value> syntax, the member names of an object or associative array can also be accessed.

Examples

// static array
numbers = array(1,2,3,4,5,6,7,8,9,10)
foreach numbers as elem
    ? elem * elem
endfor
 
// associative array
private myarray = array("Name" => "Recital", "Description" => "database")
foreach myarray as key => value
    echo "key=" + key + " value=" + value + "\n"
endfor

Replacing Sections of an Array

  • acopy() - copy elements from one array to another
logical = acopy(fromarrayname as character, toarrayname as character [, fromstartelement as numeric [, elements as numeric [, tostartelement as numeric]]])

Sorting an Array

  • asort() - sort the character elements of the specified array
logical = asort(arrayname as character [, startelement as numeric [, endelement as numeric [, reverse as numeric]]])

Passing Arrays as Function Arguments

Miscellaneous Array Functions

Converting a String Into an Array

  • astore() - fill an array from a string with separate character elements and return the number of elements
numeric = astore(arrayname, string as character, separator as character)
  • explode() - separate a character string into individual elements and return as an array
array = explode(separator as chatacter, string as character)

Converting an Array into a String

  • astring() - return an array as a character string, with the elements separated by a comma or other specified character
character = astring(arrayname [, separator as character])
  • implode() - return an array as a character string, with the elements separated by a specified character
character = implode(separator as character, arrayname)

Summary