Page 1 of 1

Bug in ADEL() ?

Posted: Sat Aug 10, 2013 12:13 pm
by serge_girard
Hello all,

Should the function ADEL() change the LEN of the array?
AADD does but ADEL doesn't:

Code: Select all

#include <hmg.ch>
#include <Dbstruct.ch>

SET PRINTER TO debug.TXT 
SET PRINTER ON 
SET CONSOLE OFF

aDbf := {}
FOR A = 1 TO 10
	cF = 'F' + ALLTRIM(STR(A))
	AADD(aDbf, { cF, "C", 5, 0 })
NEXT A
DBCREATE("TESTARR", aDbf)

USE TESTARR NEW
aStruct := TESTARR->(DBSTRUCT())

FOR j := 1 To len(aStruct)
	? STR(j) + ' ' + aStruct [j] [1] + ' ' + aStruct [j] [2] + ' ' + STR(aStruct [j] [3]) + ' ' + STR(aStruct [j] [4])
NEXT j

? STR(LEN(aStruct)) +   ' Before AADD ->LEN(aStruct)' 
AADD(aStruct,{'XX','C',5, 0})
? STR(LEN(aStruct)) +   ' After AADD  ->LEN(aStruct)' 

? '----------------------------------------' 
? STR(LEN(aStruct)) +   ' Before ADEL ->LEN(aStruct)' 
nPOS = 3
ADEL(aStruct, nPOS)      
ADEL(aStruct, nPOS)      
ADEL(aStruct, nPOS)      
? STR(LEN(aStruct)) +   ' After ADEL  ->LEN(aStruct)' 
FOR j := 1 To len(aStruct)  // gives Error BASE/1068 Argument error: array access
	? STR(j) + ' ' + aStruct [j] [1] + ' ' + aStruct [j] [2] + ' ' + STR(aStruct [j] [3]) + ' ' + STR(aStruct [j] [4])
NEXT j

SET PRINTER TO  
SET PRINTER Off
SET CONSOLE On
Anybody any idea?

Thx, Serge
[u]Moderator Notes[/u] (Pablo César) wrote:Topic moved from Forum Help.
Please note this issue is a Harbour matter and the section of "Forum Help" is used to let suggestions and claims, not for techinicals proposes of HMG or Harbour.

Bug in ADEL() ?

Posted: Sat Aug 10, 2013 1:02 pm
by Pablo César
I think the name ADEL is rather misleading, since the function does NOT actually delete elements, but rather nullifies them, and you have to use asize()[*] to achieve what you probably wanted to do. Perhaps ANUL() would be a more precise name but i think it's a little late now to rename it.

ADEL() does delete the element, it just doesn't resize the array, so you will always get a NIL at the end of the array. It's not the same as 'array[ n ] := NIL'.

Try to use AKill(aStruct, nPOS), but put at begging of prg file:

#translate AKill( <a>, <e> ) => ASize( ADel( <a>, <e> ), Len( <a> ) - 1 )

Re: Bug in ADEL() ?

Posted: Sat Aug 10, 2013 1:31 pm
by serge_girard
Thank you Pablo, it is working!

Re: Bug in ADEL() ?

Posted: Sat Aug 10, 2013 1:56 pm
by Rathinagiri
adel() function deletes a particular item and all the items behind will be pushed forward and the last array element is made nil. Pablo's function is neat and simple.

Re: Bug in ADEL() ?

Posted: Sat Aug 10, 2013 2:25 pm
by serge_girard
Yes it is!

Thx, Serge

Re: Bug in ADEL() ?

Posted: Sat Aug 10, 2013 5:42 pm
by Carlos Britos
serge_girard wrote:Hello all,

Should the function ADEL() change the LEN of the array?
AADD does but ADEL doesn't:
Hi,
try with hb_ADel( aArray, nPos, .T. )

Re: Bug in ADEL() ?

Posted: Sat Aug 10, 2013 6:43 pm
by esgici
Carlos Britos wrote:
serge_girard wrote: Should the function ADEL() change the LEN of the array?
AADD does but ADEL doesn't:
try with hb_ADel( aArray, nPos, .T. )
Harbour ChangLog; 2012-09-04] wrote:
use: hb_ADel( <aVal>, <nPos>, .t. )
instead of: ADel( <aVal>, <nPos> ); ASize( <aVal>, Len( <aVal> ) - 1 )
Or simply use ADEL() with 3.th parameter and #include "hbcompat.ch"

Code: Select all

ADel( aArray, nPos, .T. )
Sample:

Code: Select all

#include "hbcompat.ch"
a1 := { 1,2,3,4,5 }
ADel( a1, 3, .T. )
AEval( a1, { | x1, i1 | QOUT( i1, x1 ) } )
NOTE : HB_ADel() not requires #include "hbcompat.ch"

Re: Bug in ADEL() ?

Posted: Sat Aug 10, 2013 7:04 pm
by serge_girard
I'll try things tomorrow !

Thx all, Serge