Page 1 of 1

ARRAY

Posted: Mon Jul 26, 2010 11:55 pm
by MdaSolution
how I can del an element of a array and refresh the array ?
I have a array aData sample
56,.t.
102,.f.
103,.f.
I want delete only the array element with .t. and refresh the array aData

Re: ARRAY

Posted: Tue Jul 27, 2010 1:12 am
by ukoenig
// I want delete only the array element with .t. and refresh the array aData

PRIVATE aData[5][2]
aData[1] := { 56, .T. }
aData[2] := { 102, .F. }
aData[3] := { 103, .F. }
aData[4] := { 104, .T. }
aData[5] := { 105, .F. }

// Optional
PRIVATE aNEWARRAY := {}


// Show all Array-Elements
I := 1
FOR I := 1 TO LEN(aData)
Msgalert( aData[1] )
NEXT

// Delete all Elements with .T.
I := 1
Y := LEN( aData )
FOR I := 1 TO Y
IF aData[2] = .T.
ADEL( aData,I )
Y-- // Reduce Array-Length - 1 ( dynamic FOR / NEXT )
AADD(aNEWARRAY, { aData[1], .F.}) // Optional save all .F. to a new Array
ENDIF
NEXT

// Shows 102, 103, 105 ( original ARRAY with only .F.)
I := 1
FOR I := 1 TO Y ( Y = new Array-length )
Msgalert( aData[1] )
NEXT

// Optional
I := 1
FOR I := 1 TO LEN( aNEWARRAY )
Msgalert( aNEWARRAY[1] )
NEXT


Best Regards
Uwe :lol:

Re: ARRAY

Posted: Tue Jul 27, 2010 7:35 am
by nageswaragunupudi
This is simpler

Code: Select all

AEval( AClone( aData ), { |a,i| If( a[ 2 ], ADel( aData, i, .t. ), ) } )

Re: ARRAY

Posted: Tue Jul 27, 2010 9:56 am
by hua
Take note, in Rao's code, ADel() has 3 parameter. The 3rd parameter is an xHarbour extension which I think is not supported by Harbour

Re: ARRAY

Posted: Tue Jul 27, 2010 12:03 pm
by nageswaragunupudi
hua wrote:Take note, in Rao's code, ADel() has 3 parameter. The 3rd parameter is an xHarbour extension which I think is not supported by Harbour
This works with Harbour too. Just link xhb.lib. Harbour make file in samples folder includes this lib

Re: ARRAY

Posted: Tue Jul 27, 2010 10:11 pm
by mmercado
Dear Rao:
nageswaragunupudi wrote: This works with Harbour too. Just link xhb.lib. Harbour make file in samples folder includes this lib
The function in xhb.lib that does the job is named xhb_adel()

Best regards.

Manuel Mercado Gómez.

Re: ARRAY

Posted: Wed Jul 28, 2010 1:38 am
by nageswaragunupudi
Thanks Mr. Mercado.
Yes. You are right.
If we need syntax compatibility with xharbour, we need to include xhb.ch in the program file and link it with xhb.lib.

The logic I posted above is faulty.
Here is the corrected one

Code: Select all

#include "Fivewin.ch"
#ifndef __XHARBOUR__
#include "xhb.ch"
#endif

function main()

   local aData := { { 1, .t. }, { 2, .f. }, { 3, .t. }, { 4, .f. } }
   local n := 1

   xbrowse( aData )
   AEval( AClone( aData ), { |a,i| If( a[ 2 ], ADel( aData, n, .t. ), n++ ) } )
   xbrowse( aData )

return nil
 

Re: ARRAY

Posted: Wed Jul 28, 2010 8:21 am
by MdaSolution
thanks to all.
I corrected the error .