Page 1 of 1
For each explanation
Posted: Mon Mar 13, 2017 5:49 am
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
For each explanation
Posted: Mon Mar 13, 2017 10:50 am
by Pablo César
Hi Bill,
You asked for explanation but let me make some observations in your example:
- Screen124.png (8.97 KiB) Viewed 3945 times
- There is nothing that I can see with or without it.
- Returns with all lower case as it was defined.
- 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:
- 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.
- When you use $ its means that is be contained at any string.
- 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.
- 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.
Re: For each explanation
Posted: Mon Mar 13, 2017 12:54 pm
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/
Re: For each explanation
Posted: Mon Mar 13, 2017 3:08 pm
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:
means:
- take a character of string <s> (starting from the first one)
- store it in variable <c>
- use this value of <c> as argument for code found inside the loop.
- 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
For each explanation
Posted: Mon Mar 13, 2017 3:43 pm
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 (5.55 KiB) Viewed 3889 times
And thank you Bill for indication of:
FOR EACH c IN @s
Re: For each explanation
Posted: Tue Mar 14, 2017 9:15 am
by Anand
Thanks Pete for the explanation.
I also learned new things today.
Regards,
Anand
Re: For each explanation
Posted: Tue Mar 14, 2017 9:28 am
by serge_girard
Me too !
Serge