Search in two-dimensional array

Asked

Viewed 230 times

2

Hello,

I took some courses to learn how to program and I have learned advpl, I have learned to search arrays using the function aScan, but now my array is two-dimensional, as I will 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?

1 answer

1


To perform searches on 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 code block containing an advpl code snippet containing the value you want to find.


Normally you search by placing 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 it is a boolean value, you can also just deny the value:

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

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

Note: It is valid to note that advpl the indexes start at 1.


Your complete code 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 too Runtime error.


Documentation:

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

https://tdn.totvs.com/display/tec/Tipagem+de+Dados

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.