Interesting: StringFind() and StringReplace() on char "\n"

Back to topics list To post a new topic, please log in or register
avatar
193
endy5 2015.06.03 10:27
 
string s="\n"+"Bye"+"\n"+"Bye"+"\n";
Alert (StringFind(s,"\n",0)," ",StringReplace(s,"\n"," ") );

return: StringFind() --> -1

           StringReplace() --> 3

avatar
1332
jjc 2015.06.03 12:26 #
 
endy5:

[...]

I'd guess the following:

  • When processing the parameters for Alert(), MT4 evaluates the StringReplace() before the StringFind()
  • StringReplace() modifies the s variable (i.e. modifies in place, rather than returning the modified string)
  • So, StringFind() is called after StringReplace() has been called, and therefore after the \n characters have been replaced with spaces 
  • Therefore, StringFind() says that there are no \n characters in the string

avatar
193
endy5 2015.06.03 12:41 #
 
jjc:

I'd guess the following:

  • When processing the parameters for Alert(), MT4 evaluates the StringReplace() before the StringFind()
  • StringReplace() modifies the s variable (i.e. modifies in place, rather than returning the modified string)
  • So, StringFind() is called after StringReplace() has been called, and therefore after the \n characters have been replaced with spaces 
  • Therefore, StringFind() says that there are no \n characters in the string

hmmm I wrote Alert code to two rows:

Alert (StringFind(s,"\n",0));  // return: 0
Alert(StringReplace(s,"\n"," ") );  // return: 3

strange behavior MT4 (for me)

avatar
13012
WHRoeder 2015.06.03 14:54 #
 

The order that parameters are processed is not defined. Therefor your original alert did the replace first, creating a new s. Then the find proceeded, on the no longer existing string.

See int b=(a++)*3;  // invalid expression

avatar
Moderator
3206
angevoyageur 2015.06.03 15:59 #
 
endy5:

hmmm I wrote Alert code to two rows:

strange behavior MT4 (for me)

What is strange ? That's normal results.
avatar
126
Demos 2015.06.03 16:46 #
 

Once again, the MQL manual has a rather strange approach in expressing "notions". The expression int b=(a++)*3 is not invalid, as if invalid means that it will generate a compilation error.

All in all , it will work as described in that very page

The prefix increment (++i) and decrement (--k) are applied to the variable right before this variable is used in an expression.

Post-increment (a++) and post-decrement (k--) are applied to the variable right after this variable is used in an expression"

 

As an example, the following part  is working as expected

 

int a=5; int b=(a++)*5; int c=(++a)*5;
    
Alert(a," ",b," ",c);

 

Regarding OP's question, it looks like there is a "bug". I put double quotes on it because it seems that the functions are regarded as "imported";  as such they are, as  have been said, evaluated form right to left. Still, please pay attention to the manual saying regarding StringFind()

"Return Value

Returns position number in a string, from which the searched substring starts, or -1, if the substring is not found" 

 As such, it is legitimate to return 0

 

 EDIT at the manual, at page Functions > Function call, we can read

" The order of expressions calculation and the order of values loading are not guaranteed "

I guess that statement is above and beyond any rules regarding precedence rules hehe

 

best regarsds 

avatar
313
JD4 2015.06.25 01:35 #
 

Demos:

All in all , it will work as described in that very page

The prefix increment (++i) and decrement (--k) are applied to the variable right before this variable is used in an expression.

Post-increment (a++) and post-decrement (k--) are applied to the variable right after this variable is used in an expression"


" The order of expressions calculation and the order of values loading are not guaranteed "

I have seen a similar issue in other programming languages, and one possible solution is to "force" the expression(s) to be calculated in the order you want them to be is by using parenthesis (or brackets as some people call them).  Some languages actually do have a default calculation order, like all multiplication and division before all addition and subtraction, and those processed left to right in order that they appear.  But when there is no clear hierarchy, this can lead to programming headaches trying to track down a problem.  The increment and decrement examples listed above look like they function the exact same as I have seen done in Java, and I presume C as well does it this way.
Back to topics list  

To add comments, please log in or register