Equality comparison

Asked

Viewed 106 times

4

I’m studying ADVPL and came across two ways to make the equality comparison.

There are comparisons made with two signs of equal:

if cVar == "Valor"
endif

But there are also a few cases that I found, that this comparison is made with only one sign of equal:

if cVar = "Valor"
endif

There is a difference in these two ways of making an equal comparison?

  • 4

    In xbase dialects use = is a trap, as it can be either assignment or comparison (without the if instead of a syntax error will occur the equivalent of cVar := valor, and this will go unnoticed depending on the context). As @Maniero said, better := or ==. I even set up my editor to show the = Alone with a red background to avoid danger, I use Harbour a lot in everyday life. (If not known, Harbour would be more or less like ADVPL in syntax, but implemented in a very solid way, for general use and cross-platform, with backward compatibility with Clipper)

  • Thank you, I didn’t even know it was possible to do value assignment like this, I was always taught to do with :=

2 answers

4


The first form is the most correct for almost all cases. I would even ignore the second. It may seem useful, but it’s best to use a more explicit way when you need this semantics.

The second way does not compare perfect equality in texts, it compares only a part of the text ignoring the excess characters of the first, i.e., it compares the amount of characters that the right text of the operator has, he does not look at what comes after that amount in the first text, so in the example below the x will be ignored and will be considered equal when using the =.

"Valorx" = "Valor"
"Valor" == "Valor"
"Valorx" != "Valor"

If you turn on the set exact on there the = is equal to ==. But can you guarantee that in every code it is on? Not always the person can. I advise to call, even so do not use that operator.

One of the problems is that it’s easy to get it wrong. It’s usually not what you want.

For other types it does not change in relation to ==.

What’s more, when assigning a value to the variable always use := and not just = which works, but confuses the allocation operator with the partial comparison operator. For all intents and purposes ignore the =, whether to assign or to compare, use only := and == respectively.

This is a syntax defect inherited from dBase.

Documentation.

  • Thank you very much!

1

Complementing the @Maniero response by using the function aScan that searches for values in the array, the equality comparison is done only with an equal sign, just by doing an Ascan as follows:

aScan(aMeuArray, "Valor")

It may end up finding the position of the array and you have a false positive.

To search for array strings, we usually declare the code block, which allows equal checking with the two equals signals:

aScan(aMeuArray, {|cValorPosicaoArray| cValorPosicaoArray == "Valor"})

Example:

function u_Testing()
local aMeuArray as array
local nPosicao as numeric

aMeuArray := {"Valor "}

nPosicao := aScan(aMeuArray, "Valor")

//Encontrou o item no array
ConOut("Posicao encontrada:", nPosicao)

nPosicao := aScan(aMeuArray, {|cValorPosicaoArray| cValorPosicaoArray == "Valor"})

//Não encontrou o item no array
ConOut("Posicao encontrada:", nPosicao)

return
  • 1

    I believe that if the ADVPL respected the Clipper, the "set Exact" defines whether the comparison will be exact or not in the ASCan() also. Valeria a test with SET EXACT ON and SET EXACT OFF

  • Yes, the behavior of aScan is also amended in accordance with set exact :D

Browser other questions tagged

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