Page 1 of 1
Duplicar Array con registros que cumplan una condicion
Posted: Thu May 07, 2020 11:47 am
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
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
Re: Duplicar Array con registros que cumplan una condicion
Posted: Sat May 09, 2020 8:18 am
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
Re: Duplicar Array con registros que cumplan una condicion
Posted: Sun May 10, 2020 5:45 am
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.
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.
Re: Duplicar Array con registros que cumplan una condicion
Posted: Mon May 11, 2020 8:08 pm
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
Re: Duplicar Array con registros que cumplan una condicion
Posted: Tue May 12, 2020 1:46 am
by nageswaragunupudi
Code: Select all
DBEVAL( { || If( ( nAt := AScan( aGroepen, FIELD->groep ) ) == 0, AAdd( aGroepen, { FIELD->groep, 1 } ), aGroepen[ nAt, 2 ] += 1 ) } )
Re: Duplicar Array con registros que cumplan una condicion
Posted: Tue May 12, 2020 9:59 pm
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
Re: Duplicar Array con registros que cumplan una condicion
Posted: Wed May 13, 2020 3:21 pm
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.
Re: Duplicar Array con registros que cumplan una condicion
Posted: Thu May 14, 2020 6:20 pm
by Marc Venken
Works !! Thank you.