operador In

Post Reply
User avatar
goosfancito
Posts: 1392
Joined: Fri Oct 07, 2005 7:08 pm

operador In

Post 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?
Un aporte chico hace grandes cambios.
Apoyemos al proyecto "Hogar pimpinela"
Bajate la aplicación (gratuita) y encuentra en ella toda la info de como podes colaborar.
GRACIAS!
https://play.google.com/store/apps/deta ... .acomprar
User avatar
jvtecheto
Posts: 357
Joined: Mon Mar 04, 2013 4:32 pm
Location: Spain

Re: operador In

Post 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.
Fwh 19.06 32 bits + Harbour 3.2dev(r2011030937)+ Borland 7.4 + FivEdit
User avatar
nageswaragunupudi
Posts: 8017
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Contact:

Re: operador In

Post 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.
Regards

G. N. Rao.
Hyderabad, India
User avatar
jvtecheto
Posts: 357
Joined: Mon Mar 04, 2013 4:32 pm
Location: Spain

Re: operador In

Post 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.
Fwh 19.06 32 bits + Harbour 3.2dev(r2011030937)+ Borland 7.4 + FivEdit
User avatar
karinha
Posts: 4882
Joined: Tue Dec 20, 2005 7:36 pm
Location: São Paulo - Brasil

Re: operador In

Post by karinha »

João Santos - São Paulo - Brasil
User avatar
nageswaragunupudi
Posts: 8017
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Contact:

Re: operador In

Post 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.
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
Posts: 8017
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Contact:

Re: operador In

Post 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.
Regards

G. N. Rao.
Hyderabad, India
Post Reply