4D v13.4

Arrays and Pointers

Home

 
4D v13.4
Arrays and Pointers

Arrays and Pointers  


 

 

You can pass an array as parameter to a 4D command or to the routine of a 4D Plug-in. On the other hand, you cannot pass an array as parameter to a user method. The alternative is to pass a pointer to the array as parameter to the method.

You can pass process and interprocess, process or local arrays as parameters.

Here are some examples.

  • Given this example:
 If((0<atNames)&(atNames<Size of array(atNames))
  ` If possible, selects the next element to the selected element
    atNames:=atNames+1
 End if

If you need to do the same thing for 50 different arrays in various forms, you can avoid writing the same thing 50 times, by using the following project method:

  ` SELECT NEXT ELEMENT project method
  ` SELECT NEXT ELEMENT ( Pointer )
  ` SELECT NEXT ELEMENT ( -> Array )
 
 C_POINTER($1)
 
 If((0<$1->)&($1-><Size of array($1->))
    $1->:=$1->+1 ` If possible, selects the next element to the selected element
 End if

Then, you can write:

 SELECT NEXT ELEMENT(->atNames)
  ` ...
 SELECT NEXT ELEMENT(->asZipCodes)
  ` ...
 SELECT NEXT ELEMENT(->alRecordIDs)
  ` ... and so on
  • The following project method returns the sum of all the elements of a numeric array (Integer, Long Integer, or real):
  ` Array sum
  ` Array sum ( Pointer )
  ` Array sum ( -> Array )
 
 C_REAL($0)
 
 $0:=0
 For($vlElem;1;Size of array($1->))
    $0:=$0+$1->{$vlElem}
 End for

Then, you can write:

 $vlSum:=Array sum(->arSalaries)
  ` ...
 $vlSum:=Array sum(->aiDefectCounts)
  ` ..
 $vlSum:=Array sum(->alPopulations)
  • The following project method capitalizes of all the elements of a string or text array:
  ` CAPITALIZE ARRAY
  ` CAPITALIZE ARRAY ( Pointer )
  ` CAPITALIZE ARRAY ( -> Array )
 
 For($vlElem;1;Size of array($1->))
    If($1->{$vlElem}#"")
       $1->{$vlElem}:=Uppercase($1->{$vlElem}≤1≥)+Lowercase(Substring($1->{$vlElem};2))
    End if
 End for

Then, you can write:

 CAPITALIZE ARRAY(->atSubjects)
  ` ...
 CAPITALIZE ARRAY(->asLastNames)

The combination of arrays, pointers, and looping structures, such as For...End for, allows you to write many useful small project methods for handling arrays.

 
PROPERTIES 

Product: 4D
Theme: Arrays

 
SEE ALSO 

Arrays
Arrays and the 4D Language