Page 1 of 1

operador In

Posted: Sun Feb 21, 2021 11:28 pm
by goosfancito
Hola.

Code: Select all

local aDatos:={1,2,3}
if (x in aDatos)
   ? "esta"
endif
 
eso al ejecutarlo me da un error de argumento, que hago mal?

Re: operador In

Posted: Mon Feb 22, 2021 9:53 am
by jvtecheto
Hola Gustavo.

El operador In ahora me entero que existe en Harbour, creo poder afirmar que en Clipper no existia.

si fueran cadenas podrias utilizar el operador $

de la forma

Code: Select all

   local x := "a"
   local aDatos := "abc"
    if (x $ aDatos)
       ? "esta"
    endif

 
pero con numeros, como sabes puedes utilizar

Code: Select all

   local x := 1
  local aDatos := {1,2,3}
    if (Ascan(aDatos, x)>0)
       ? "esta"
    endif
 
Saludos

Jose.

Re: operador In

Posted: Mon Feb 22, 2021 10:15 am
by nageswaragunupudi

Code: Select all

x IN aDatos 
 
is permissible syntax in xHarbour and is very powerful.
I do not understand the reason for the runtime error, because we can use any datatype for x in this syntax.

Re: operador In

Posted: Mon Feb 22, 2021 10:59 am
by jvtecheto
Mr. Rao

I think Gustavo is using Harbour , the operator IN only exists in XHarbour

in Harbour and with arrays should uses

Code: Select all

(Ascan(aDatos, x)>0)
 
Regards

Jose.

Re: operador In

Posted: Mon Feb 22, 2021 1:23 pm
by karinha

Re: operador In

Posted: Mon Feb 22, 2021 5:13 pm
by nageswaragunupudi
I think Gustavo is using Harbour , the operator IN only exists in XHarbour
If he is using Harbour, he should get a compilation error but not runtime error.

Re: operador In

Posted: Tue Feb 23, 2021 3:10 am
by nageswaragunupudi
Now, I understand.

This sample program:

Code: Select all

#include "fivewin.ch"
#include "hbcompat.ch"

function Main()

   local x     := 2
   local aList := { 1,2,3 }

   if ( x IN aList )
      ? "ok"
   else
      ? "not ok"
   endif

return nil
When compiled and built with xHarbour works perfectly.

When we compile with Harbour, the hbcompat.ch uses this translate to preprocess:

Code: Select all

   #translate ( <exp1> IN <exp2> )     => ( ( <exp1> ) $ ( <exp2> ) )
 
So,

Code: Select all

   if ( x IN aList )
 
is translated and compiled as:

Code: Select all

if ( x $ aList )
 
This naturally results in runtime error.