FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin para Harbour/xHarbour Duplicar Array con registros que cumplan una condicion
Posts: 297
Joined: Fri Apr 14, 2006 05:52 PM
Duplicar Array con registros que cumplan una condicion
Posted: Thu May 07, 2020 11:47 AM
Buenas Tardes Foro:

Ante todo, espero que esteis todos bien en este confinamiento. Necesitaria que me orientasis con una utilidad que necesito, y seria la siguiente. A partir de un array bidimensional, me gustaria llamar a una funcion genérica, pasandole el array y una condicion, y que esta funcion me devolviera el array con los elementos que cumplan esa condicion. Ejemplo practico:

Code (fw): Select all Collapse
             {.t.,"Test1",100},
             {.f.,"Test2",200},
             {.f.,"Test3",300},
             {.t.,"Test4",400},


Code (fw): Select all Collapse
             Condicion --> Columna 1 fuese .t.


La funcion me devuelve

Code (fw): Select all Collapse
             {.t.,"Test1",100},
             {.t.,"Test4",400}


o por ejemplo

Code (fw): Select all Collapse
             Condicion --> Columna 1 fuese .t. y Columna 3 < 200


La funcion me devuelve

Code (fw): Select all Collapse
             {.t.,"Test1",100}



Me he peleado con macros y eval, aeval, etc... Creo que habrá algo, y para no reinventar la rueda, os rogaria me echarais un cable.

Un Saludo y muchas gracias de antemano
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: Duplicar Array con registros que cumplan una condicion
Posted: Sat May 09, 2020 08:18 AM
Code (fw): Select all Collapse
function TestGetSubArray()

   local aArray   := { ;
      {.t.,"Test1",100}, ;
      {.f.,"Test2",200}, ;
      {.f.,"Test3",300}, ;
      {.t.,"Test4",400}  }

   XBROWSER GetSubArray( aArray, { |a| a[ 1 ] } )
   XBROWSER GetSubArray( aArray, { |a| a[ 1 ] .and. a[ 3 ] < 200 } )

return nil



function GetSubArray( aArray, bCond )

   local aRet  := {}

   AEval( aArray, { |a| If( Eval( bCond, a ), AAdd( aRet, a ), nil ) } )

return aRet  // or AClone( aRet ) if you want an independent array
Regards



G. N. Rao.

Hyderabad, India
Posts: 297
Joined: Fri Apr 14, 2006 05:52 PM
Re: Duplicar Array con registros que cumplan una condicion
Posted: Sun May 10, 2020 05:45 AM
Hello Nages:

Thanks for your reply. It worked perfectly. The mistake I made at the beginning was that the second parameter was not passed to the EVAL function.

:-)

Code (fw): Select all Collapse
   AEval( aArray, { |a| If( Eval( bCond, a ), AAdd( aRet, a ), nil ) } )

   My mistake

   AEval( aArray, { |aDoc| If( Eval( bCondition,<<<<aDoc>>>>), AAdd( aProff, aDoc ), nil ) } )


Many thanks.
Posts: 1487
Joined: Tue Jun 14, 2016 07:51 AM
Re: Duplicar Array con registros que cumplan una condicion
Posted: Mon May 11, 2020 08:08 PM
Also a question about array but now dbeval
Code (fw): Select all Collapse
function Test
   local aGroepen:={}

   use mailbulk NEW VIA "DBFCDX"
   mailbulk->(dbsetorder("email"))
   mailbulk->(dbgotop())
   DBEVAL( { || If( AScan( aGroepen, FIELD->groep ) == 0, AAdd( aGroepen, FIELD->groep ), nil ) } )
   ASORT( aGroepen )


This will give me the array of all existing groep's (names for a pulldown)

But I would like to have a count for how many times it was in the database (or shoul I make a do while.....)

Result now = "Work","School","Private", but i would like "Work (21)","School (15)","Pricate (5)" (number) it the times it exist in dbf
Marc Venken

Using: FWH 23.08 with Harbour
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: Duplicar Array con registros que cumplan una condicion
Posted: Tue May 12, 2020 01:46 AM
Code (fw): Select all Collapse
DBEVAL( { || If( ( nAt := AScan( aGroepen, FIELD->groep ) ) == 0, AAdd( aGroepen, { FIELD->groep, 1 } ), aGroepen[ nAt, 2 ] += 1 ) } )
Regards



G. N. Rao.

Hyderabad, India
Posts: 1487
Joined: Tue Jun 14, 2016 07:51 AM
Re: Duplicar Array con registros que cumplan una condicion
Posted: Tue May 12, 2020 09:59 PM
nageswaragunupudi wrote:
Code (fw): Select all Collapse
DBEVAL( { || If( ( nAt := AScan( aGroepen, FIELD->groep ) ) == 0, AAdd( aGroepen, { FIELD->groep, 1 } ), aGroepen[ nAt, 2 ] += 1 ) } )


Thanks, but the result is not a array with only 1 occurence and a total of times it exists, but a large array with all rec's

Maybe I have to define the array otherwise , since it is now 2 dimensions ?
Maybe the Ascan is not looking at the right place ? Did some tests, but no better result
Marc Venken

Using: FWH 23.08 with Harbour
Posts: 2170
Joined: Fri Jul 18, 2008 01:24 AM
Re: Duplicar Array con registros que cumplan una condicion
Posted: Wed May 13, 2020 03:21 PM
Marc Venken wrote:
nageswaragunupudi wrote:
Code (fw): Select all Collapse
DBEVAL( { || If( ( nAt := AScan( aGroepen, FIELD->groep ) ) == 0, AAdd( aGroepen, { FIELD->groep, 1 } ), aGroepen[ nAt, 2 ] += 1 ) } )


Thanks, but the result is not a array with only 1 occurence and a total of times it exists, but a large array with all rec's

Maybe I have to define the array otherwise , since it is now 2 dimensions ?
Maybe the Ascan is not looking at the right place ? Did some tests, but no better result


Try this way:

Code (fw): Select all Collapse
DBEVAL( { || If( ( nAt := AScan( aGroepen, {|a| a[1] == FIELD->groep} ) ) == 0,;
                 AAdd( aGroepen, { FIELD->groep, 1 } ), aGroepen[ nAt, 2 ] += 1 ) } )

Regards.
Francisco J. Alegría P.

Chinandega, Nicaragua.



Fwxh-MySql-TMySql
Posts: 1487
Joined: Tue Jun 14, 2016 07:51 AM
Re: Duplicar Array con registros que cumplan una condicion
Posted: Thu May 14, 2020 06:20 PM

Works !! Thank you.

Marc Venken

Using: FWH 23.08 with Harbour

Continue the discussion