For each explanation

Topic Specific Tutorials and Tips.

Moderator: Rathinagiri

Post Reply
bluebird
Posts: 103
Joined: Wed Sep 28, 2016 3:55 am
Has thanked: 22 times
Been thanked: 5 times

For each explanation

Post by bluebird » 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

User avatar
Pablo César
Posts: 4058
Joined: Wed Sep 08, 2010 1:18 pm
Location: Curitiba - Brasil
Has thanked: 100 times
Been thanked: 177 times

Post by Pablo César » Mon Mar 13, 2017 10:50 am

Hi Bill,

You asked for explanation but let me make some observations in your example:
 
Screen124.png
Screen124.png (8.97 KiB) Viewed 626 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: 4346
Joined: Wed Jul 30, 2008 9:17 pm
DBs Used: DBF
Location: iskenderun / Turkiye
Has thanked: 232 times
Been thanked: 80 times
Contact:

Post by esgici » Mon Mar 13, 2017 12:54 pm

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

User avatar
PeteWG
Posts: 107
Joined: Sun Mar 21, 2010 5:45 pm
Has thanked: 3 times
Been thanked: 27 times

Post by PeteWG » Mon Mar 13, 2017 3:08 pm

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 am
What 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: 4058
Joined: Wed Sep 08, 2010 1:18 pm
Location: Curitiba - Brasil
Has thanked: 100 times
Been thanked: 177 times

Post by Pablo César » Mon Mar 13, 2017 3:43 pm

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 570 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: 208
Joined: Tue May 24, 2016 4:36 pm
DBs Used: DBF
Has thanked: 23 times
Been thanked: 33 times

Post by Anand » Tue Mar 14, 2017 9:15 am

Thanks Pete for the explanation.

I also learned new things today.

Regards,

Anand
Image

User avatar
serge_girard
Posts: 1865
Joined: Sun Nov 25, 2012 2:44 pm
DBs Used: 1 MySQL - MariaDB
2 DBF
Location: Belgium
Has thanked: 292 times
Been thanked: 67 times
Contact:

Post by serge_girard » Tue Mar 14, 2017 9:28 am

Me too !

Serge

Post Reply