For each explanation

Topic Specific Tutorials and Tips.

Moderator: Rathinagiri

Post Reply
bluebird
Posts: 172
Joined: Wed Sep 28, 2016 3:55 am
DBs Used: DBF

For each explanation

Post by bluebird »

Dear Mentor

The following excerpt from HMG for beginners does not realy explain what seems to be a powerful loop device
Can anyone provide a better example and explanation?

Does "c IN @s" mean "character in both strings"? What is the at sign for
Thanks

Local c := "alles", s := "abcdefghijk"
FOR EACH c IN @s
   IF c $ "aei"
      c := UPPER( c )
   ENDIF
NEXT
? s      // AbcdEfghIjk
? s      // alles
User avatar
Pablo César
Posts: 4059
Joined: Wed Sep 08, 2010 1:18 pm
Location: Curitiba - Brasil

For each explanation

Post by Pablo César »

Hi Bill,

You asked for explanation but let me make some observations in your example:
 
Screen124.png
Screen124.png (8.97 KiB) Viewed 3913 times
 
  1. There is nothing that I can see with or without it.
     
  2. Returns with all lower case as it was defined.
     
  3. I think you mean c instead s.
 
I've refaced your example and I noticed that:
 

Code: Select all

#include <hmg.ch>

Function Main()
LOCAL c := "alles", s := "abcdefghijk"

FOR EACH c IN s
    IF c $ "aei"
       c := UPPER( c )
       MsgDebug(c) // AEI
    ENDIF
NEXT
MsgDebug(s)      // abcdEfghIjk
MsgDebug(c)      // alles
Return Nil
 
My POV:
  1. First at all, I wish to clear up when we use this routine is coming from Harbour not from HMG. It's our dialect language xBase that empires and it's nothing with HMG.
     
  2. When you use $ its means that is be contained at any string.
     
  3. The c var in your example it's in trying to be re-assigned but it has momentainly assigned because it's previous been taken the var in the address memory (let's say as reference, often used with @) but it can't when back at begining of FOR EACH. You can see the added MsgDebug(c) that I putted in.
     
  4. The power of FOR EACH with IN and the $ operator is a binary relational. Nothing else.
 
Sorry if I do not see more than you have expected it.
Last edited by Pablo César on Mon Mar 13, 2017 3:21 pm, edited 1 time in total.
HMGing a better world
"Matter tells space how to curve, space tells matter how to move."
Albert Einstein
User avatar
esgici
Posts: 4543
Joined: Wed Jul 30, 2008 9:17 pm
DBs Used: DBF
Location: iskenderun / Turkiye
Contact:

Re: For each explanation

Post by esgici »

bluebird wrote: Mon Mar 13, 2017 5:49 am Dear Mentor

The following excerpt from HMG for beginners does not realy explain what seems to be a powerful loop device
Can anyone provide a better example and explanation?

Does "c IN @s" mean "character in both strings"? What is the at sign for
Thanks

Local c := "alles", s := "abcdefghijk"
FOR EACH c IN @s
   IF c $ "aei"
      c := UPPER( c )
   ENDIF
NEXT
? s      // AbcdEfghIjk
? s      // alles

https://vivaclipper.wordpress.com/2012/ ... next-loop/
Viva INTERNATIONAL HMG :D
PeteWG
Posts: 176
Joined: Sun Mar 21, 2010 5:45 pm

Re: For each explanation

Post by PeteWG »

Hi,
bluebird wrote: Mon Mar 13, 2017 5:49 am Does "c IN @s" mean "character in both strings"?
FOR EACH <var> IN <expression> is an iterative loop similar to traditional FOR .. NEXT
but offers some interesting new features and depending on the case, it's more flexible and faster.

now, in our case:

Code: Select all

   FOR EACH c IN @s 
means:
  1. take a character of string <s> (starting from the first one)
  2. store it in variable <c>
  3. use this value of <c> as argument for code found inside the loop.
  4. repeat steps 1..3 until all characters of <s> have been passed to <c> and processed
bluebird wrote: Mon Mar 13, 2017 5:49 amWhat is the at sign for
The "at" <@> symbol means that members of <s> are passed to <c> and therefore to the code inside loop, by reference.
this means that any change of value of <c> inside the loop, will reflect back to string <s>, permanently changing its content.
If you don't use the <@> the string <s> will remain unaltered no matter what happens with <c> inside loop.
Note that if an array (instead of string) or hash is used as IN <expression>, their elements are always passed by reference.

For a more detailed reference, see (between others) and this link: FOR EACH ... NEXT loop

regards,

---
Pete
User avatar
Pablo César
Posts: 4059
Joined: Wed Sep 08, 2010 1:18 pm
Location: Curitiba - Brasil

For each explanation

Post by Pablo César »

PeteWG wrote: Mon Mar 13, 2017 3:08 pm The "at" <@> symbol means that members of <s> are passed to <c> and therefore to the code inside loop, by reference.
this means that any change of value of <c> inside the loop, will reflect back to string <s>, permanently changing its content.
If you don't use the <@> the string <s> will remain unaltered no matter what happens with <c> inside loop.
Note that if an array (instead of string) or hash is used as IN <expression>, their elements are always passed by reference.
Thank you Pete.

Very interesting. New result considering @ on s
Screen125.png
Screen125.png (5.55 KiB) Viewed 3857 times
And thank you Bill for indication of:

FOR EACH c IN @s
HMGing a better world
"Matter tells space how to curve, space tells matter how to move."
Albert Einstein
User avatar
Anand
Posts: 595
Joined: Tue May 24, 2016 4:36 pm
DBs Used: DBF

Re: For each explanation

Post by Anand »

Thanks Pete for the explanation.

I also learned new things today.

Regards,

Anand
Regards,

Anand

Image
User avatar
serge_girard
Posts: 3161
Joined: Sun Nov 25, 2012 2:44 pm
DBs Used: 1 MySQL - MariaDB
2 DBF
Location: Belgium
Contact:

Re: For each explanation

Post by serge_girard »

Me too !

Serge
There's nothing you can do that can't be done...
Post Reply