Página 1 de 1
For EACH aItem.....???
Enviado: 18 Nov 2009 14:09
por Ale SB
Pessoar...
tirem-me uma duvida...
Oq eh o EACH ? Usado no For/Next ?
arr:={"one", "two", "three"}
FOR EACH v IN arr
? v
v := UPPER(v)
NEXT
Eu tenho Usado aqui o Harbour e vi em alguns Topicos la no Nabble, q me parece q ele nao eh muito recomendado, nao tenho certeza, meu ingles eh ruim e o tradudor nao foi muito claro.
Uso ele ou nao ?
Qual a vantagem de usar ele ?
nao seria a mesma coisa de usar um AsCan ?
@braços Ale
Re: For EACH aItem.....???
Enviado: 18 Nov 2009 19:58
por Itamar M. Lins Jr.
Até agora não parei para estudar esse comando mas respondendo a sua pergunta.
Código: Selecionar todos
### NEW LANGUAGE STATEMENTS ###
=====================================
1. FOR EACH
Harbour support all xHarbour functionality and it offers also additional
features which are not available in xHarbour.
a) it allows to iterate more then one variable
FOR EACH a, b, c IN aVal, cVal, hVal
? a, b, c
NEXT
b) it allows to set descending order by DESCEND flag, f.e.:
FOR EACH a, v IN aVal, cVal DESCEND
? a, b
NEXT
c) it has native support for hashes:
FOR EACH x IN { "ABC" => 123, "ASD" => 456, "ZXC" => 789 }
? x, "@", x:__enumKey()
NEXT
d) it allows to assign string items, f.e.:
s := "abcdefghijk"
FOR EACH c IN @s
IF c $ "aei"
c := UPPER( c )
ENDIF
NEXT
? s // AbcdEfghIjk
e) it gives OOP interface to control enumerator variables what
is very important when more then one variable is iterated or
when FOR EACH is called recursively, f.e.:
hVal := { "ABC" => 123, "ASD" => 456, "ZXC" => 789 }
FOR EACH x IN hVal
? x:__enumIndex(), ":", x:__enumKey(), "=>", x:__enumValue(), ;
"=>", x:__enumBase()[ x:__enumKey() ]
NEXT
f) it gives very flexible OOP mechanism to overload FOR EACH behavior
for user define objects adding to above enumerator methods also
__enumStart(), __enumStop(), __enumSkip() methods what allows to
implement many different enumeration algorithms depending on used
data
g) it does not have any hardcoded limitations for recursive calls
(it's limited only by available memory and HVM stack size), f.e.:
proc main()
p( 0 )
return
proc p( n )
local s := "a", x
? n
if n < 1000
for each x in s
p( n + 1 )
next
endif
return
In xHarbour there is function HB_ENUMINDEX() which is supported by
Harbour in XHB library.
Então pode usar porque se no xHarbour o pessoal usa, no Harbour está melhor ainda.
Saudações,
Itamar M. Lins Jr.
Re: For EACH aItem.....???
Enviado: 19 Nov 2009 07:36
por MARINI
Uso muito no Xailer.
Por exemplo, temos que tornar lVisible := .T. em 5 objetos.
Em vez de fazer:
::oObj1:lVisible := .T.
::oObj2:lVisible := .T.
::oObj3:lVisible := .T.
::oObj4:lVisible := .T.
::oObj5:lVisible := .T.
ou
::oObj1:lVisible := ::oObj2:lVisible := ::oObj3:lVisible := ::oObj4:lVisible := ::oObj5:lVisible := .T.
Podemos fazer assim:
LOCAL i,xOjb:={::oObj1,::oObj2,::oObj3,::oObj4,::oObj5}
FOR EACH i IN xObj
__ObjSendMsg( i , "_lVisible", .T.)
NEXT
Traduzindo seria o mesmo que estivessemos dizendo: PARA CADA i NO xObj faça isto.
Re: For EACH aItem.....???
Enviado: 19 Nov 2009 07:54
por Ale SB
Blz Marini..
To começando entender a funçao do For Each...mas...no caso de seu exemplo ai..eu tb poderia usar um Aeval para repassar o valor .t. neh ?!
@braços Ale