Page 1 of 1

Asort array base two elements and found the last number

Posted: Sun Mar 22, 2020 1:06 am
by Silvio.Falconi
Dear friends,

I must found the last number (field 1) of this array if the cCode ( field 2) is "01"

Local aData:= { ;
{4,"01","17/03/2020","17/03/2020"} ,;
{7,"02","17/03/2020","18/03/2020"} ,;
{7,"01","17/03/2020","18/03/2020"} ,;
{11,"02","17/03/2020","18/03/2020"} ,;
{8,"01","17/03/2020","18/03/2020"} }

local cCodice:="01"

I try with

ASort( aData ,,, {|x,y| If ( x[2] == cCodice, x[1]< y[1],) } )


How I can make to found the last value for the code "01" ?

it must return me 8

Re: Asort array base two elements and found the last number

Posted: Sun Mar 22, 2020 7:31 am
by MarcoBoschi
Silvio,
Try RASCAN

Code: Select all

FUNCTION MAIN()
LOCAL x , y  , nPos
LOCAL aData:= { { 4 ,"01" , "17/03/2020" , "17/03/2020" } , ;
                { 7 ,"02" , "17/03/2020" , "18/03/2020" } , ;
                { 7 ,"01" , "17/03/2020" , "18/03/2020" } , ;
                {11 ,"02" , "17/03/2020" , "18/03/2020" } , ;
                { 8 ,"01" , "17/03/2020" , "18/03/2020" } }

LOCAL cCodice:="01"


ASORT( aData ,,, {|x,y| If ( x[2] == cCodice, x[1]< y[1],) } )

FOR x := 1 TO LEN( aData )
    ? aData[ x , 1 ] , aData[ x , 2 ] ,aData[ x , 3 ] ,aData[ x , 4 ]
NEXT x

nPos := RASCAN( aData, { | aData| aData[ 2 ] = cCodice } )

? nPos , aData[ nPos , 1 ] , aData[ nPos , 2 ] ,aData[ nPos , 3 ] ,aData[ nPos  , 4 ]

RETURN NIL
 

Re: Asort array base two elements and found the last number

Posted: Sun Mar 22, 2020 10:38 am
by Silvio.Falconi
thanks, I understood that I am doing something bigger than my abilities and when I have a problem that is easy for you it becomes difficult for me, perhaps because the mind is occupied by other thoughts and worries and I do not find the right solution