Two-dimensional Arrays

4D - Documentation   Français   English   German   Spanish   4D v11 SQL, Command Theme List   4D v11 SQL, Command Alphabetical List   4D v11 SQL, Constant Theme List   Back   Previous   Next

version 6.0


Each of the array declaration commands can create or resize one-dimensional or two-dimensional arrays. Example:

   ARRAY TEXT (atTopics;100;50) ` Creates a text array composed of 100 rows of 50 columns

Two-dimensional arrays are essentially language objects; you can neither display nor print them.

In the previous example:

atTopics is a two-dimensional array

atTopics{8}{5} is the 5th element (5th column...) of the 8th row

atTopics{20} is the 20th row and is itself a one-dimensional array

Size of array(atTopics) returns 100, which is the number of rows

Size of array(atTopics{17}) returns 50, which the number of columns for the 17th row

In the following example, a pointer to each field of each table in the database is stored in a two-dimensional array:

   C_LONGINT($vlLastTable;$vlLastField)
   C_LONGINT($vlFieldNumber)
      ` Create as many rows (empty and without columns) as there are tables
   $vlLastTable:=Get last table number
   ARRAY POINTER (<>apFields;$vlLastTable;0)  `2D array with X rows and zero columns
      ` For each table
   For ($vlTable;1;$vlLastTable)
      If(Is table number valid($vlTable))
         $vlLastField:=Get last field number($vlTable) 
            ` Give value of elements
         $vlColumnNumber:=0
         For ($vlField;1;$vlLastField)
            If(Is field number valid($vlTable;$vlField))
               $vlColumnNumber:=$vlColumnNumber+1
                  `Insert a column in a row of the table underway
               INSERT IN ARRAY(<>apFields{$vlTable};$vlColumnNumber;1)
                  `Assign the "cell" with the pointer
               <>apFields{$vlTable}{$vlColumnNumber}:=Field($vlTable;$vlField)
            End if 
         End for
      End if 
   End for

Provided that this two-dimensional array has been initialized, you can obtain the pointers to the fields for a particular table in the following way:


      ` Get the pointers to the fields for the table currently displayed at the screen:
   COPY ARRAY (<>apFields{Table(Current form table)};$apTheFieldsIamWorkingOn)
      ` Initialize Boolean and Date fields
   For ($vlElem;1;Size of array($apTheFieldsIamWorkingOn))
      Case of
         : (Type($apTheFieldsIamWorkingOn{$vlElem}->)=Is Date)
            $apTheFieldsIamWorkingOn{$vlElem}->:=Current date
         : (Type($apTheFieldsIamWorkingOn{$vlElem}->)=Is Boolean)
            $apTheFieldsIamWorkingOn{$vlElem}->:=True
      End case
   End for
   

Note: As this example suggests, rows of a two-dimensional arrays can be the same size or different sizes.

See Also

Arrays.


4D - Documentation   Français   English   German   Spanish   4D v11 SQL, Command Theme List   4D v11 SQL, Command Alphabetical List   4D v11 SQL, Constant Theme List   Back   Previous   Next