Duplicar Array con registros que cumplan una condicion

Duplicar Array con registros que cumplan una condicion

Postby Mike Serra » 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: Select all  Expand view  RUN

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


Code: Select all  Expand view  RUN

             Condicion --> Columna 1 fuese .t.
 


La funcion me devuelve

Code: Select all  Expand view  RUN

             {.t.,"Test1",100},
             {.t.,"Test4",400}
 


o por ejemplo

Code: Select all  Expand view  RUN

             Condicion --> Columna 1 fuese .t. y Columna 3 < 200
 


La funcion me devuelve

Code: Select all  Expand view  RUN

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

Re: Duplicar Array con registros que cumplan una condicion

Postby nageswaragunupudi » Sat May 09, 2020 8:18 am

Code: Select all  Expand view  RUN
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
User avatar
nageswaragunupudi
 
Posts: 10646
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: Duplicar Array con registros que cumplan una condicion

Postby Mike Serra » Sun May 10, 2020 5: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.

:cry:

Code: Select all  Expand view  RUN


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

Re: Duplicar Array con registros que cumplan una condicion

Postby Marc Venken » Mon May 11, 2020 8:08 pm

Also a question about array but now dbeval
Code: Select all  Expand view  RUN

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.04 with Harbour
User avatar
Marc Venken
 
Posts: 1439
Joined: Tue Jun 14, 2016 7:51 am
Location: Belgium

Re: Duplicar Array con registros que cumplan una condicion

Postby nageswaragunupudi » Tue May 12, 2020 1:46 am

Code: Select all  Expand view  RUN
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
nageswaragunupudi
 
Posts: 10646
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: Duplicar Array con registros que cumplan una condicion

Postby Marc Venken » Tue May 12, 2020 9:59 pm

nageswaragunupudi wrote:
Code: Select all  Expand view  RUN
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.04 with Harbour
User avatar
Marc Venken
 
Posts: 1439
Joined: Tue Jun 14, 2016 7:51 am
Location: Belgium

Re: Duplicar Array con registros que cumplan una condicion

Postby FranciscoA » Wed May 13, 2020 3:21 pm

Marc Venken wrote:
nageswaragunupudi wrote:
Code: Select all  Expand view  RUN
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  Expand view  RUN
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
User avatar
FranciscoA
 
Posts: 2159
Joined: Fri Jul 18, 2008 1:24 am
Location: Chinandega, Nicaragua, C.A.

Re: Duplicar Array con registros que cumplan una condicion

Postby Marc Venken » Thu May 14, 2020 6:20 pm

Works !! Thank you.
Marc Venken
Using: FWH 23.04 with Harbour
User avatar
Marc Venken
 
Posts: 1439
Joined: Tue Jun 14, 2016 7:51 am
Location: Belgium


Return to FiveWin para Harbour/xHarbour

Who is online

Users browsing this forum: Google [Bot] and 44 guests