FOR EACH's object

114 views
Skip to first unread message

Quique

unread,
Nov 24, 2022, 2:17:31 PM11/24/22
to Harbour Users
Hi

Does anyone know how I can create an object like a FOR EACH?

FOR EACH a IN { 1, 2, 3 }
   ? a:__enumValue      <===== I need this objec
   ? a:__enumIndex()
   ? a
NEXT

Something like that

a := object( { 1, 2, 3 }[ 2 ] )
? a:__enumValue
? a:__enumIndex()
? a

Daniele Campagna

unread,
Nov 25, 2022, 9:28:05 AM11/25/22
to harbou...@googlegroups.com

IIRC you can simply link xhb.lib - xHarbour has this syntax.

Or, you can see how it is implemented scavenging through the sources of xhb...

Dan

--
You received this message because you are subscribed to the Google Groups "Harbour Users" group.
Unsubscribe: harbour-user...@googlegroups.com
Web: https://groups.google.com/group/harbour-users
---
You received this message because you are subscribed to the Google Groups "Harbour Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to harbour-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/harbour-users/07fddab3-421a-444e-9711-8f7d0233f3c5n%40googlegroups.com.
-- 
Informativa sulla privacy disponibile all'URL http://www.appliserver.com/privacy.html

José M. C. Quintas

unread,
Nov 25, 2022, 9:34:58 AM11/25/22
to harbou...@googlegroups.com
I do not understand why you can not use a.

Do you want to use like this?

cText := "ABCDEF"

FOR EACH @cChar IN cText

   IF cChar == "C"

      cChar := "X"

   ENDIF

NEXT

José M. C. Quintas

Quique

unread,
Nov 25, 2022, 1:11:53 PM11/25/22
to Harbour Users
I need an object similar to FOR EACH but created manually, in fact, I tried to copy it, but it doesn't work

b := { "a",  "b",  "c" }   // could be  cText := "ABCDEF" like your sample, I need it too.
FOR EACH a IN b
   c := a
   ? a
   ? c
   ? a:__enmuIndex
   ? c:__enumIndex   // cause error
NEXT 

FOR EACH a IN b
   ? a
   ? a:__enmuIndex
   prueba( @a )   // i tried  prueba( a )   too
NEXT 

function prueba( c )
   ? c
   ? c:__enumIndex   // cause error
return nil

Really I need an specific value, I tried this test trying to do it

b := { "a",  "b",  "c" }
FOR EACH a IN b
   IF a:__enumIndex == 2
      c := a
      EXIT
   ENDIF
NEXT
? c
? c:__enumIndex   // cause error

Now, taking advantage of this need, the possibility of creating my own object with my own methods occurs to me, without losing the ability to use the variable in a simple way such as FOR EACH.

But hey, that would be something I could take advantage of, for now, what I need is for it to work like the FOR EACH object is working.

José M. C. Quintas

unread,
Nov 25, 2022, 1:19:59 PM11/25/22
to harbou...@googlegroups.com

Another option:

b := { "a",  "b",  "c" }

FOR EACH a IN b

   Routine( a, a:__EnumIndex )

NEXT

José M. C. Quintas


--
You received this message because you are subscribed to the Google Groups "Harbour Users" group.
Unsubscribe: harbour-user...@googlegroups.com
Web: https://groups.google.com/group/harbour-users
---
You received this message because you are subscribed to the Google Groups "Harbour Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to harbour-user...@googlegroups.com.

Quique

unread,
Nov 25, 2022, 1:21:57 PM11/25/22
to Harbour Users
I  look it up in src and in contrib/xhb, but I don't know if it's just my poor command of C, but I couldn't find a way to do it. 

I found __enumidex in classes.c, but from what I understood, that's for initializing the object engine.

W.

unread,
Nov 25, 2022, 5:01:18 PM11/25/22
to Harbour Users
Such custom object example can be found here:
Regards
w.

Quique

unread,
Nov 25, 2022, 5:44:26 PM11/25/22
to Harbour Users
It is a very interesting example, but it is to work an object in a FOR EACH, in this case, what I need to create is the object of the letter e, it is very easy to make a class with the variables __enumIndex, __enumValue, but the problem is using the variable as a variable, not as an object.

I can use the variable as

? a 
a := 3

I just pass the element to it as a reference and it directly modifies the element of the array, but 

? to:__enumIndex 
? to:__enumValue 

they would mark an error, because the variable does not have them, and vice versa, if I create the object, the second part works, but the first part does not. 

What I want is to be able to make use of both options as the variables within a FOR EACH.

Quique

unread,
Nov 25, 2022, 6:10:15 PM11/25/22
to Harbour Users
My problem is the following, you have to realize that we have this (is a simple example, my structure is much more complex, but the idea is this):

function ejemplo()
FOR EACH cNombre, nEdad, cSexo IN aNombres, aEdades, aSexos
   .
   .
? cNombre, nEdad, cSexo
nEdad++
? cSexo:__enumIndex()
   .
   .
NEXT

FOR EACH cNombre, nEdad, cSexo IN aNombres, aEdades, aSexos
? cNombre, nEdad, cSexo
NEXT
return nil

The problem is that you have to divide 3 functions that will be called independently, but for the example we can only divide and call from FOR EACH, in the real structure that I have, I cannot handle a FOR EACH, precisely, because of the independent call of each function, but that's the least of it, if I could take the object directly from a FOR EACH, I consider myself well served, I already lost a lot of time finding an alternative solution.

function ejemplo()
FOR EACH cNombre, nEdad, cSexo IN aNombres, aEdades, aSexos
   dividida( cNombre, nEdad, cSexo )
NEXT
despues( aNombres, aEdades, aSexos )

function dividida( cNombre, nEdad, cSexo )
   .
   .
? cNombre, nEdad, cSexo
nEdad++
? cSexo:__enumIndex()   <==== It causes error
   .
   .
return nil

function despues( aNombres, aEdades, aSexos )
FOR EACH cNombre, nEdad, cSexo IN aNombres, aEdades, aSexos
? cNombre, nEdad, cSexo
NEXT
return nil

Ok, maybe it's easy to send the __enumIdex, but that already makes me modify the code, which I don't want to do, besides that if I'm working with hashes, I would have to send the __enumKey and __enumValue, and on the other hand, I still don't we have identified all the FOR EACH that will be modified, so we need to do it in all those places, the idea is if we can create an object like the one used by the FOR EACH, we only have to send the objects and the rest of the code intact.

Harbor already does it, the problem is that I don't know how.
Reply all
Reply to author
Forward
0 new messages