How I can delete an item …

How I can delete an item in an array ?

Short Answer : We have a special function : ADel()
Long Answer :

ADel() function may cause some confusions when documentation not read carefully:

This function deletes the element found at the given subscript position in the array.

All elements in the array below the given subscript position will move up one positionin the array.

 

Let’s try:

?
 aTest1 := { 'One', 'Two', "Tree",'Four' }
 aTest := ACLONE( aTest1 )
 ADel( aTest, 3 )
 ? LEN( aTest )
 AEVAL( aTest, { | c1 | QQOUT( c1, '' ) } ) 
 

Result :
4
One Two Four NIL
Length of array not changed, last item moved up and new last item became NIL.

 

?
 ASIZE( aTest, 3 )
 ? LEN( aTest )
 ?
 AEVAL( aTest, { | c1 | QQOUT( c1, '' ) } )

Result :
3
One Two Four
Length of array changed, last item (NIL) removed

We have another possibility: a new Harbour function:

?
 aTest1 := { 'One', 'Two', "Tree",'Four' }
 aTest := ACLONE( aTest1 )
 HB_ADel( aTest, 3 )
 ? LEN( aTest )
 ?
 AEVAL( aTest, { | c1 | QQOUT( c1, '' ) } )
 

Result :
4
One Two Four NIL
Length of array not changed, last item moved up and new last item became NIL.
Same as ADel()

And we have a third parameter : Shrink or not:

 ?
 aTest1 := { 'One', 'Two', "Tree",'Four' }
 aTest := ACLONE( aTest1 )
 HB_ADel( aTest, 3, .T. )
 AEVAL( aTest, { | c1 | QQOUT( c1, '' ) } )

That’s all !

 

How I can insert a new …

How I can insert  a new item to an array ?

Short answer : We have a special function : AIns()
Long Answer :

AIns() function may cause some confusions when documentation not read carefully:

This function inserts a NIL value in the array named <aArray> at the <nPos>th position.

All array elements starting with the <nPos>th position will be shifted down one subscript position in the array list and the last item in the array will be removed completely.

 

Let’s try:

 aTest := { 'One', 'Two', 'Four' }
 AIns( aTest, 3 ) 
 AEVAL( aTest, { | c1 | QQOUT( c1, '' ) } ) 
 

Result : One Two NIL: “Four” lost !

Since it’s NIL, we can assign a value to it:

 aTest := { 'One', 'Two', 'Four' }
 AIns( aTest, 3 )
 aTest[ 3 ] := "Three"
 AEVAL( aTest, { | c1 | QQOUT( c1, '' ) } ) 
 

Result : One Two Three: “Four” lost !

Before insert we can add a dummy item to end of array:

 aTest := { 'One', 'Two', 'Four' }
 AADD( aTest, NIL )
 AIns( aTest, 3 )
 aTest[ 3 ] := "Three"
 AEVAL( aTest, { | c1 | QQOUT( c1, '' ) } ) 
 

Result : One Two Three Four : OK
Or we can change size of array :

 aTest := { 'One', 'Two', 'Four' }
 ASIZE( aTest, 4 )
 AIns( aTest, 3 )
 aTest[ 3 ] := "Three"
 AEVAL( aTest, { | c1 | QQOUT( c1, '' ) } ) 
 

Result : One Two Three Four : OK

But wait; we have another possibility: a new Harbour function:

 aTest := { 'One', 'Two', 'Four' }
 HB_AIns( aTest, 3, "Three" )
 AEVAL( aTest, { | c1 | QQOUT( c1, '' ) } ) 

Result : One Two Three: “Four” lost !

And we have a fourth parameter : Grow or not:

 aTest := { 'One', 'Two', 'Four' }
 HB_AIns( aTest, 3, "Three", .T. )
 AEVAL( aTest, { | c1 | QQOUT( c1, '' ) } ) 
 

Result : One Two Three Four

That’s all !