KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Exporting a Collection as text
PRODUCT: 4D | VERSION: 16R5 | PLATFORM: Mac & Win
Published On: February 15, 2018

Here is a utility method to export a Collection as text:

// ----------------------------------------------------------------------
// Name: EXPORT_COLLECTION_AS_TEXT
// Description: Method will take a collection and return the contents as
// a text separated by lines.
//
// Parameters:
// $1 (POINTER) - Pointer to a collection
// Output:
// $0 (TEXT) - TEXT
// E.g. [0]:123
// [1]:false
// [2]:test
// [3]:{"prop":"value"}
// ----------------------------------------------------------------------
C_POINTER($1;$col)
C_TEXT($0;$colText)
C_LONGINT($i)

if (Count parameters=1)
 $col:=$1

 For ($i;0;$col->length-1)
  If (Value type($col->[$i])=Is object)
   $colText:=$colText+"["+String($i)+"]:"+json stringify($col->[$i])+Char(13)+Char(10)
  Else
   $colText:=$colText+"["+String($i)+"]:"+String($col->[$i])+Char(13)+Char(10)
  End if

 End for
End if

$0:=$colText


Here is an example of using the method:

C_COLLECTION($col)
C_OBJECT($obj)
C_TEXT($colAsText)

$col:=New collection
OB SET($obj;"prop";"value")

$col.push(123)
$col.push(false)
$col.push("test")
$col.push($obj)

$colAsText:=EXPORT_COLLECTION_AS_TEXT (->$col)
// [0]:123
// [1]:false
// [2]:test
// [3]:{"prop":"value"}