Duplicar Array con registros que cumplan una condicion

Post Reply
Mike Serra
Posts: 287
Joined: Fri Apr 14, 2006 5:52 pm
Location: Córdoba (España)

Duplicar Array con registros que cumplan una condicion

Post by Mike Serra »

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: Select all

             {.t.,"Test1",100},
             {.f.,"Test2",200},
             {.f.,"Test3",300},
             {.t.,"Test4",400},
 

Code: Select all

             Condicion --> Columna 1 fuese .t.
 
La funcion me devuelve

Code: Select all

             {.t.,"Test1",100},
             {.t.,"Test4",400}
 
o por ejemplo

Code: Select all

             Condicion --> Columna 1 fuese .t. y Columna 3 < 200
 
La funcion me devuelve

Code: Select all

             {.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
User avatar
nageswaragunupudi
Posts: 8017
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Contact:

Re: Duplicar Array con registros que cumplan una condicion

Post by nageswaragunupudi »

Code: Select all

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
Mike Serra
Posts: 287
Joined: Fri Apr 14, 2006 5:52 pm
Location: Córdoba (España)

Re: Duplicar Array con registros que cumplan una condicion

Post by Mike Serra »

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.

:cry:

Code: Select all


   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.
User avatar
Marc Venken
Posts: 727
Joined: Tue Jun 14, 2016 7:51 am

Re: Duplicar Array con registros que cumplan una condicion

Post by Marc Venken »

Also a question about array but now dbeval

Code: Select all

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 20.08 with Harbour
User avatar
nageswaragunupudi
Posts: 8017
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Contact:

Re: Duplicar Array con registros que cumplan una condicion

Post by nageswaragunupudi »

Code: Select all

DBEVAL( { || If( ( nAt := AScan( aGroepen, FIELD->groep ) ) == 0, AAdd( aGroepen, { FIELD->groep, 1 } ), aGroepen[ nAt, 2 ] += 1 ) } )
Regards

G. N. Rao.
Hyderabad, India
User avatar
Marc Venken
Posts: 727
Joined: Tue Jun 14, 2016 7:51 am

Re: Duplicar Array con registros que cumplan una condicion

Post by Marc Venken »

nageswaragunupudi wrote:

Code: Select all

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 20.08 with Harbour
User avatar
FranciscoA
Posts: 1964
Joined: Fri Jul 18, 2008 1:24 am
Location: Chinandega, Nicaragua, C.A.

Re: Duplicar Array con registros que cumplan una condicion

Post by FranciscoA »

Marc Venken wrote:
nageswaragunupudi wrote:

Code: Select all

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: Select all

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.

Fwxh1204-MySql-TMySql
User avatar
Marc Venken
Posts: 727
Joined: Tue Jun 14, 2016 7:51 am

Re: Duplicar Array con registros que cumplan una condicion

Post by Marc Venken »

Works !! Thank you.
Marc Venken
Using: FWH 20.08 with Harbour
Post Reply