Function Len() Foundation

Determines the length of a character string or an array.

Syntax
Len( <cString> | <aArray> | <oObject> ) --> nCount
Parameters
<cString>
<cString> is a character string whose length is determined.
<aArray>
<aArray> is an array whose size (number of elements) is determined.
<oObject>
<oObject> is an object whose number of exported instance member variables is determined.
Return

Len() returns an integer numeric value. The function returns zero if the parameter contains no elements.

Description

The Len() function returns the number of elements of the passed parameter. If a character string is passed, each character of a character string is counted, including Chr(0). An empty character string (a null string "") has the length zero.

If an array is passed, Len() determines the number of elements in the first dimension of the array. To determine the total number of array elements in a multi-dimensional array, each subarray must be passed to the function Len() and the return values must be totaled. An empty array has the length zero.

If an object is passed, Len() determines the object's exported instance member variables and returns their count. If the object has no instance member variables or if they are protected or hidden, the function returns zero.

Examples
Len()
// The example shows various results of the function Len(). 

PROCEDURE Main 
   LOCAL oObj 

   ? Len("Xbase++")                 // result: 7 
   ? Len("")                        // result: 0 
   ? Len(Chr(0))                    // result: 1 
   ? Len( SaveScreen() )            // result: 4000 

   ? Len( {1,2,3,4,5}   )           // result: 5 
   ? Len( {1,2,{3,4,5}} )           // result: 3 

   oObj := DataObject():new() 
   oObj:var1 := "Test" 
   oObj:var2 := 12 
   ? Len( oObj )                    // result: 2 
RETURN 
Len() and FOR...NEXT

// The example shows one of the most frequent uses of Len() 
// with an array. It is used to determine the end of a FOR...NEXT 
// loop. In the example the field names and contents of a 
// data record are displayed. 

PROCEDURE Main 
   LOCAL aStruct, n, nEnd 
   USE Address NEW 

   aStruct := DbStruct() 
   nEnd    := Len( aStruct ) 

   FOR n := 1 TO nEnd 
      ? aStruct[n,1], ":", FieldGet(n) 
   NEXT 

   USE 
RETURN 
Total number of elements in a multi-dimensional array

// In this example the total number of elements in a 
// multi-dimensional array is determined. 

PROCEDURE Main 

   ? TotalElements( Directory() ) 

RETURN 

FUNCTION TotalElements( aArray ) 
   LOCAL n, nLen, nTotal 

   nLen   := Len( aArray ) 
   nTotal := 0 

   FOR n := 1 TO nLen 
      IF Valtype( aArray[n] ) == "A" 
         nTotal += TotalElements( aArray[n] ) 
      ELSE 
         nTotal ++ 
      ENDIF 
   NEXT 

RETURN nTotal 

Feedback

If you see anything in the documentation that is not correct, does not match your experience with the particular feature or requires further clarification, please use this form to report a documentation issue.