Two-dimensional array search

Hello,

I did some courses to learn how to program and I have learned advpl, I learned how to do searches in arrays using the function aScan, but now my array is two-dimensional, how will I do the search?

I have for example this array:

local aTest := { { 0, "Teste", .T. }, { 1, "Teste", .F. } }

//Se eu faço o busca dessa forma, o valor não é encontrado
//local nPos := aScan( aTest, .F. ) 

Is there any way to search in a two-dimensional array?

Author: Daniel Mendes, 2020-04-13

1 answers

To perform searches in a two-dimensional array, you also use the function aScan, but as a second parameter you do not put exactly the value you want to find, but rather a block of code containing a snippet of advpl code containing the value you want to find.


Normally you search by putting as second parameter the value you want to find, right?

local nPos := aScan( aTest, .F. ) 

But since the array is two-dimensional, you need to access a specific position of it in the search, then enter the Code Block:

local nPos := aScan( aTest, {| aPosArray | aPosArray[3] == .F. } ) 

Since this is a Boolean value, you can also just deny the value:

local nPos := aScan( aTest, {| aPosArray | !aPosArray[3] } )

Imagine that this block will be executed twice, which is the size of your array, the variable aPosArray will receive the value of your main array, and the first time it will have the value { 0, "Teste", .T. } and the second time the value { 1, "Teste", .F. }

Obs.: It is valid to point out that advpl indexes start at 1.


Your complete code it would look more or less like this:

user function pesqArray()
local aTest as array
local nPos as numeric

aTest := { { 0, "Teste", .T. }, { 1, "Teste", .F. } }
nPos := aScan( aTest, {| aPosArray | !aPosArray[3] } )

ConOut("O valor foi encontrado na posicao:", nPos)

return

In the example I also typed the variables, this helps to catch small errors based on the warnings that the compiler returns, since advpl ends up having a lot of runtime error.


Documentation:

Https://tdn.totvs.com/display/tec/AScan

Https://tdn.totvs.com/display/tec/Tipagem + of + dice

Https://tdn.totvs.com/pages/viewpage.action?pageId=6063094

 1
Author: Daniel Mendes, 2020-06-11 14:45:34