open database southwind
use shippers
copy to arrayofobjects aShipObjects for shipperid < 3 ? aShipObjects Array (refcnt=1) ( [1] = Dynarray ( [shipperid] => 1
[companyname] => Speedy Express
[phone] => (503) 555-9831
)
[2] = Dynarray
(
[shipperid] => 2
[companyname] => United Package
[phone] => (503) 555-3199
)
)
https://www.lianja.com/doc/index.php/COPY_TO_ARRAYOFOBJECTS
declare orders[10000,10]
use suppliers
copy to array orders for ord_date < date()
https://www.lianja.com/doc/index.php/COPY_TO_ARRAY
use suppliers
declare cust[reccount(), fcount()]
copy to array cust
copy structure to customer
use customer
append from array cust for also_cust = .T.
https://www.lianja.com/doc/index.php/APPEND_FROM_ARRAY
DECLARE
DIMENSION
Dynamic arrays are declared without specifying a size. Elements are added using arrayname.element syntax.
aDynarray.name = [Lianja Inc] aDynarray.email = [sales@lianja.com]
They can then be referenced by element number or by element name.
? aDynarray.name Lianja Inc ? aDynarray[2] sales@lianja.com
Notes: The brackets shown for this command do not indicate optional expressions but are a necessary part of the syntax. Arrays can also be declared using any of the scoping commands: LOCAL, PRIVATE and PUBLIC.
// Declare one-dimensional array of 4000 elements declare aTable[4000] // Assign 0 to all elements aTable = 0 // Insert individual element values into array aTable[1] = 10 aTable[2] = "Hello" aTable[3] = "World" aTable[4] = date() // Print value of element 2 ? aTable[2] Hello // Another example declare twodim[3,3] twodim[2,3] = "hello world" ? twodim[6] hello world // Another example use payroll declare aPayroll[reccount(), fcount()] copy to array aPayroll for city = "LONDON" // Dynamic array declare aDynarray[] aDynarray.name = [Lianja Inc] ? aDynarray.name Lianja Inc ? aDynarray[1] Lianja Inc
https://www.lianja.com/doc/index.php/DECLARE
ARRAY
customer = array("custname" => "A Buyer Ltd.", ; "currentorder" => array("ord_id" => "00001", "ord_date" => date(),"ord_total" => 1599,; "orderitems" => array("10 reams A4 paper","500 business cards",; "500 black ballpoint pens"))) display memory Memory Variables: ------------------------ CUSTOMER Dynamic array (refptr ARRAY, refcnt 1) .CUSTNAME Character 'A Buyer Ltd.' .CURRENTORDER Dynamic array (refptr ARRAY, refcnt 0) .ORD_ID Character '00001' .ORD_DATE Date 11/04/2009 .ORD_TOTAL Numeric 1599 (1599.000000000) .ORDERITEMS Dynamic array (refptr ARRAY, refcnt 0) .00000001 Character '10 reams A4 paper' .00000002 Character '500 business cards' .00000003 Character '500 black ballpoint pens' Total of 10 variables defined and 784 bytes used.
https://www.lianja.com/doc/index.php/ARRAY()
declare array1[3]
array1 = "test"
? alen(array1)
3
aadd(array1,"newtest")
? array1[4]
newtest
https://www.lianja.com/doc/index.php/AADD()
use payroll
declare number[reccount(),1]
copy to array number fields pay_value
? aaverage(number)
1741.01
https://www.lianja.com/doc/index.php/AAVERAGE()
declare overview[4000] declare north[2000] // Copy first 2000 elements from source array // starting from position 1 in the target array acopy(north,overview,1,2000,1) declare south[1500] // Copy first 1500 elements from source array // starting from position 2001 in the target array acopy(south,overview,1,1500,2001)
https://www.lianja.com/doc/index.php/ACOPY()
element_no = ascan(flist,"New information") // If element exists if m.element_no > 0 adel(flist,m.element_no) endif // Two-dimensional array Examples: // Row open database southwind use shippers copy to array arr1 ? arr1 // Delete row 2 adel(arr1,2) ? arr1 close data // Results: Array (refcnt=1) ( [1,1] = 1 [1,2] = Speedy Express [1,3] = (503) 555-9831 [2,1] = 2 [2,2] = United Package [2,3] = (503) 555-3199 [3,1] = 3 [3,2] = Federal Shipping [3,3] = (503) 555-9931 ) Array (refcnt=1) ( [1,1] = 1 [1,2] = Speedy Express [1,3] = (503) 555-9831 [2,1] = 3 [2,2] = Federal Shipping [2,3] = (503) 555-9931 [3,1] = False [3,2] = False [3,3] = False ) // Column open database southwind use shippers copy to array arr1 ? arr1 // Delete column 2 adel(arr1,2,2) ? arr1 close data // Results: Array (refcnt=1) ( [1,1] = 1 [1,2] = Speedy Express [1,3] = (503) 555-9831 [2,1] = 2 [2,2] = United Package [2,3] = (503) 555-3199 [3,1] = 3 [3,2] = Federal Shipping [3,3] = (503) 555-9931 ) Array (refcnt=1) ( [1,1] = 1 [1,2] = (503) 555-9831 [1,3] = False [2,1] = 2 [2,2] = (503) 555-3199 [2,3] = False [3,1] = 3 [3,2] = (503) 555-9931 [3,3] = False )
https://www.lianja.com/doc/index.php/ADEL()
close data open database southwind use employees copy to array names eExpression = "Rob" nStartElement = 5 nElementsSearched = 104 nSearchColumn = 0 element_no=ascan(names, eExpression, nStartElement, nElementsSearched) if element_no > 0 ? names[element_no] ? element_no else ? "Not found" endif // Exact on element_no=ascan(names, eExpression, nStartElement, nElementsSearched, nSearchColumn, 6) if element_no > 0 ? names[element_no] ? element_no else ? "Not found" endif nStartRow = 3 nRowsSearched = 6 nSearchColumn = 3 element_no=ascan(names, eExpression, nStartRow, nRowsSearched, nSearchColumn) if element_no > 0 ? names[element_no] ? element_no else ? "Not found" endif // Return row number, exact off row_no=ascan(names, eExpression, nStartRow, nRowsSearched, nSearchColumn, 10) if row_no > 0 ? names[row_no, nSearchColumn] ? row_no else ? "Not found" endif // Return row number, exact on row_no=ascan(names, eExpression, nStartRow, nRowsSearched, nSearchColumn, 14) if row_no > 0 ? names[row_no, nSearchColumn] ? row_no else ? "Not found" endif
https://www.lianja.com/doc/index.php/ASCAN()
declare test[4,4]
? aelement(test, 3,2)
10
https://www.lianja.com/doc/index.php/AELEMENT()
declare matrix[100] afill(matrix,"good",51,100) ? matrix[45] .F. ? matrix[75] good ? afill(matrix,"bad",1,50) .T. ? matrix[45] bad
https://www.lianja.com/doc/index.php/AFILL()
declare table[10] ains(table, 3, "hello world") // Another Example declare flist[5] ... if ascan(flist, "new information") = 0 ains(flist,1,"new information") endif // Two-dimensional array open database southwind use shippers copy to array arr1 ? arr1 ains(arr1,1) ? arr1 close data // Results Array (refcnt=1) ( [1,1] = 1 [1,2] = Speedy Express [1,3] = (503) 555-9831 [2,1] = 2 [2,2] = United Package [2,3] = (503) 555-3199 [3,1] = 3 [3,2] = Federal Shipping [3,3] = (503) 555-9931 ) Array (refcnt=1) ( [1,1] = False [1,2] = False [1,3] = False [2,1] = 1 [2,2] = Speedy Express [2,3] = (503) 555-9831 [3,1] = 2 [3,2] = United Package [3,3] = (503) 555-3199 )
https://www.lianja.com/doc/index.php/AINS()
declare aNames[10,12] ? alen(aNames) 120 ? alen(aNames,0) 120 ? alen(aNames,1) 10 ? alen(aNames,2) 12
https://www.lianja.com/doc/index.php/ALEN()
use payroll
declare number[reccount(),1]
copy to array number fields total
? amax(number)
8165.19
https://www.lianja.com/doc/index.php/AMAX()
use payroll
declare number[reccount(),1]
copy to array number fields total
? amin(number)
16.23
https://www.lianja.com/doc/index.php/AMIN()
declare array1[100] ? alen(array1) 100 asize(array1,200) ? alen(array1) 200 declare array2[100,5] ? alen(array2) 500 ? alen(array2,1) 100 asize(array2, alen(array2,1)+1) ? alen(array2) 505 ? alen(array2,1) 101
https://www.lianja.com/doc/index.php/ASIZE()
use accounts
declare names[reccount(),1]
copy to array names fields company
asort(names)
https://www.lianja.com/doc/index.php/ASORT()
declare aNums[10] ? alen(aNums) 10 nelements = ASTORE(aNums, "one, two, three", ",") ? nelements 3 ? alen(aNums) 3 // Another Example menu fields select "+" declare aMenu[512] nelements = astore(aMenu, menuitem(), "+")
https://www.lianja.com/doc/index.php/ASTORE()
menu fields select "+"
declare aMenu[512]
nelements = astore(aMenu, menuitem(), "+")
backtostring = astring(aMenu, "+")
https://www.lianja.com/doc/index.php/ASTRING()
use state.rdb
private aRECORDS[50,fcount()]
copy to array aRECORDS
nELEM = ascan(aRECORDS,'California')
? asubscript(aRECORDS,nELEM,1)
6
https://www.lianja.com/doc/index.php/ASUBSCRIPT()
use payroll
declare aNumber[reccount(),1]
copy to array aNumber fields pay_amount
? asum(aNumber)
127641.73
https://www.lianja.com/doc/index.php/ASUM()
tab = array("day" => "Monday", "month" => "May")
// Return "2014" if no 'year' element exists
? getmember("tab","year","2014")
https://www.lianja.com/doc/index.php/GETMEMBER()
open database southwind
use example
declare names[reccount(),1]
copy to array names fields start_date
valid_date = in_array({02/09/2000}, names)
https://www.lianja.com/doc/index.php/IN_ARRAY()
open database southwind select * from example into array afixed ? is_array(afixed) .T. select * from example into object adynamic ? is_array(adynamic) .T.
https://www.lianja.com/doc/index.php/IS_ARRAY()
// ali.prg - 4 lines cVar=filetostr("ali.prg") nVar = alines(aVar, cVar) echo "Program has " + ltrim(str(nVar)) + " lines\n" // Another Example declare aNums[10] nelements = alines(aNums, "one, two, three", 0, ",")
https://www.lianja.com/doc/index.php/ALINES()
menu fields select "+"
aMenu = explode("+", menuitem())
backtostring = implode("+", aMenu)
https://www.lianja.com/doc/index.php/IMPLODE()
menu fields select "+"
aMenu = explode("+", menuitem())
backtostring = implode("+", aMenu)
https://www.lianja.com/doc/index.php/EXPLODE()
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
Associative Arrays
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. The key identifiers follow the standard naming rules (see naming under Simple Variables above).
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
https://www.lianja.com/doc/index.php/A_Recital_Primer
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
Associative Arrays
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. The key identifiers follow the standard naming rules (see naming under Simple Variables above).
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
https://www.lianja.com/doc/index.php/A_Lianja_Primer
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] // or dimension 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
Accessing Elements in a Static Array
Elements in a static array are accessed using the element number, or the element subscripts in the case of two-dimensional arrays. Array element numbers and subscripts start from 1 and are specified in square or round brackets after the array name. Row and column subscripts are separated by a comma: [row,column].
? array1[3] ? array2[2,4] ? array3(5,2)
Associative Arrays
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. The key names conform to the standard identifier naming rules: an identifier must begin with a letter (A-Z, a-z) or an underscore (-), followed by any combination of letters, digits or underscores. The identifier can be of any length, but only the first 32 characters are significant, so these must be unique.
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["doj"] = date() // Alternative syntax private tab[] tab.name = "bill" tab.age = 25 tab.doj = date()
Associative arrays can be created and initialized in one statement using the array( ) function.
tab = array("name" => "bill", "age" => 25, ""doj" => date())
You can view the contents of an associative array using the echo or ? commands.
? tab
Accessing Elements in an Associative Array
Like static arrays, associative arrays can also be accessed via their element number in square brakets:
private tab[] tab.name = "bill" tab.age = 25 tab.doj = date() ? tab[2] 25
However, they are most commonly accessed via their key:
? tab.age 25
When setting the element value, the key must be used.
tab["age"] = 26
Looping Through Arrays
// 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" => "Lianja", "Description" => "database") foreach myarray as key => value echo "key=" + key + " value=" + value + "\n" endfor
Passing Arrays as Function Arguments
Both static and dynamic arrays are passed to functions by reference, allowing the original of the array to be updated by the called function.
function func1 parameter p1 p1.age = 27 return .t. private tab[] tab.name = "Bill" tab.age = 25 func1(tab) echo tab.age, "\n" 27
https://www.lianja.com/doc/index.php/Lianja_Arrays