|
Arrays
Arrays make it possible for you to refer to a series of
variables by the same name and to use an index to tell them apart.
This helps you create smaller and simpler code in many situations,
because you can set up loops that deal efficiently with any number
of cases by using the index number. Arrays are useful when you must
store a number of values, but you do not know how many, or you do
not want to create individual variables to store them all.
For example, suppose you must store a numeric value for every
day of the year. You could declare 365 separate numeric variables,
but that would be a lot of work. Instead, you can create an array
to store all the data in one variable. The array itself is a single
variable with multiple elements; each element can contain one piece
of data.
You can use loops, together with a couple of special functions
for working with arrays, to assign values to or retrieve values
from the various elements of an array.
Creating array
To declare an array variable use the following statement:
array A
All arrays are dynamically resizable. So you don't have to worry
about the size of array.
Assigning array variable
The following example creates 30 elements in the array A
and assigns the values to array elements
array A
inti
for (i=0;
i<30; i++)
A[i] =
i
endfor
Accessing array variables
The following example demonstrates how array
variable can be accessed:
array A
inti
for (i=0;
i<30; i++)
A[i] =
i
endfor
for (i=0;
i<30; i++)
Println(A[i])
endfor
When the size of array is unknown the size can be
obtained using GetArraySize() function:
array A
inti
for (i=0;
i<30; i++)
A[i] =
i
endfor
intnSize = GetArraySize(A)
for (i=0; i<nSize;
i++)
Println(A[i])
endfor
Unknown arrays can be traversed. The following
example demonstrate such technique:
arrayA =
GetArray() # some
function that returns array
intnSize = GetArraySize(A)
intnPosition = 0
stringsIndex
for(nPosition =
0; nPosition < nSize; nPosition
++)
# Position
represents the phisical location of the array element
# sIndex is
the real index of the array element
sIndex = GetArrayIndex(A,
nPosition)
Println(A[sIndex])
endfor
Using multidimensional arrays
You can create multidimensional arrays. A multidimensional array
uses more than one index to access data. Multidimensional arrays
are arrays of arrays, where each sub-array can have a different
length.
For example, two-dimensional array presents the table. The
following example demonstrates how to use two-dimensional arrays to
query the database. There is table 'Person' in the database that
contains the following data:
| Record #
|
First Name
|
Last Name
|
| 0 |
George |
Washington |
| 1 |
John |
Kennedy |
stringsConnectionString = "DSN=PRESIDENTS"
intnID = DBOpen(sConnectionString)
if( nID > 0)
array Result = DBSelect(nID, "select * from Person")
if (DBGetSQLStatus(nID) != SQLOK)
Println(DBGetSQLErrorString(nID))
else
int nPosition
string sIndex
for(nPosition=0; nPosition < GetArraySize(Result),;
nPosition++)
sIndex =
GetArrayIndex(Result, nPosition)
Println(Result[sIndex]["FirstName"], " ",
Result[sIndex]["LastName"])
endfor
endif
endif
DBClose(nID)
The output of the script:
George Washington
John Kennedy
|