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 !