This is the legacy 4D documentation web site. Documentations are progressively being moved to developer.4d.com

Home

 
4D v19.8
collection.insert( )

collection.insert( ) 


 

collection.insert ( index ; element ) -> Function result 
Parameter Type   Description
index  Longint in Where to insert the element
element  Expression in Element to insert in the collection
Function result  Collection in Original collection containing inserted element

The collection.insert( ) method inserts element at the specified index position in the collection instance and returns the edited collection.

Note: This method modifies the original collection.

In index, pass the position where you want the element to be inserted in the collection.

Warning: Keep in mind that collection elements are numbered from 0.

  • If index > the length of the collection, actual starting index will be set to the length of the collection.
  • If index <0, it is recalculated as index:=index+length (it is considered as the offset from the end of the collection).
  • If the calculated value is negative, index is set to 0.

Any type of element accepted by a collection can be inserted, even another collection.

Example  

 C_COLLECTION($col)
 $col:=New collection("a";"b";"c";"d") //$col=["a","b","c","d"]
 $col.insert(2;"X") //$col=["a","b","X","c","d"]
 $col.insert(-2;"Y") //$col=["a","b","X","Y","c","d"]
 $col.insert(-10;"Hi") //$col=["Hi","a","b","X","Y","c","d"]



See also 

collection.combine( )

 
PROPERTIES 

Product: 4D
Theme: Collections

 
PAGE CONTENTS 
 
HISTORY 

Created: 4D v16 R6

 
ARTICLE USAGE

4D Language Reference ( 4D v19)
4D Language Reference ( 4D v19.1)
4D Language Reference ( 4D v19.4)
4D Language Reference ( 4D v19.5)
4D Language Reference ( 4D v19.6)
4D Language Reference ( 4D v19.7)
4D Language Reference ( 4D v19.8)