View Full Version : Using dynamic/asssociative arrays

2012-02-29, 00:37
The Recital/VFP implementation in Lianja brings many modern language features to VFP including support for PHP-style dynamic/associative arrays using the ARRAY( ) function. This function operates in the same way as the PHP ARRAY( ) function. It can be used to declare a dynamic or associative array and optionally initialize it with elements. It can be used to store key/value pairs within your Apps.

// declare an empty dynamic array
a = array()

// declare a simple dynamic array
a = array("barry", "recital", "boston")
foreach a as value
? value

// to add new elements to a dynamic array do this
a[] = "london"
a[] = date()
a[] = 21

// declare an associative array
a = array("name" => "barry", "company" => "recital", "location" => "boston")
? "length of a is " + len(a)
foreach a as key => value
? "key=" + key + ", value=" + value

// associative arrays act just like objects and we can access their members using common OO syntax
? a.name
? a.company

// alternaively you can look up values like this
? a["name"]
? a["company"]

// adding new key/value pairs is simple
a[ "age" ] = 21
a[" date" ] = date()

// to inspect a dynamic array just do this
? a

Note that associative array elements can themselves be arrays or other associative arrays or objects.

All arrays and objects are reference counted and will only be released by the garbage collector when their are no references to them.