Function ATail() Foundation

Retrieves the contents of the last element in an array.

Syntax
ATail( <aArray> ) --> xValue
Parameters
<aArray>
<aArray> is the array whose last element is returned.
Return

The return value of ATail() is the contents of the last element in the array.

Description

The array function ATail() returns the contents of the last element in an array. The function serves as an abbreviation for the expression <aArray>[Len(<aArray>)].

Examples
ATail()
// The example demonstrates the return value of ATail() 

PROCEDURE Main 
   LOCAL aArray1, aArray2 

   aArray1 := {"A","B","C","D","E"} 
   aArray2 := { 1 , 2 , 3 , 4 , 5 } 

   ? ATail( aArray1 )                 // result: E 
   ? ATail( aArray2 )                 // result: 5 
RETURN 

Push and pop functions for arrays

// In the example a pseudofunction APush() is shown 
// which stacks values in an array. The UDF APop() 
// retrieves the last value from the stack (array) 
// using ATail() and then removes the element. 

#xtranslate APush( <aArray>,<xValue> ) => ; 
          AAdd( <aArray>,<xValue> ) 

PROCEDURE Main 
   LOCAL aStack:={} 

   APush( aStack, "Three" ) 
   APush( aStack, "Two"   ) 
   APush( aStack, "One"   ) 

   ? Len( aStack )             // result: 3 
   ? APop(aStack )             // result: One 

   ? Len( aStack )             // result: 2 
   ? APop(aStack )             // result: Two 

   ? Len( aStack )             // result: 1 
   ? APop(aStack )             // result: Three 

   ? Len( aStack )             // result: 0 
   ? APop(aStack )             // result: NIL 

RETURN 

FUNCTION APop( aArray ) 
   LOCAL xValue 
   IF Len( aArray ) > 0 
      xValue := ATail( aArray ) 
      ASize( aArray, Len(aArray)-1 ) 
   ENDIF 
RETURN xValue 

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.