Insert array elements value inside a loop

 

Hello everybody,

I am a newbie coming from php programming. I have some problems with the array in mql, even if I already read the online doc and articles.

I use a lot the array in my programming, but even the most simple operation it looks more complicated than php.

I would like to insert the values of an array elements inside a loop.

string firstArr[7];
int size = ArraySize(currency);

   for( int i = 0; i < size; i++ ){
   
      if( StringFind(currency[i], firstCurrency, 0) != -1 ){
      
         // push the element into the first available index of firstArr
         // like in php i do with
         // firstArr[] = currency[i];
      
      }
   
   }

thanks a lot for helping!

 
somtam:

Hello everybody,

I am a newbie coming from php programming. I have some problems with the array in mql, even if I already read the online doc and articles.

I use a lot the array in my programming, but even the most simple operation it looks more complicated than php.

I would like to insert the values of an array elements inside a loop.

thanks a lot for helping!

You have to learn how arrays are working with mql5. It's very different than php. See this article : https://www.mql5.com/en/articles/567

MQL5 Programming Basics: Arrays
MQL5 Programming Basics: Arrays
  • 2013.03.11
  • Dmitry Fedoseev
  • www.mql5.com
Arrays are an integral part of almost any programming language along with variables and functions. The article should be of interest primarily to novice MQL5 programmers, while experienced programmers will have a good opportunity to summarize and systematize their knowledge.
 
angevoyageur:

You have to learn how arrays are working with mql5. It's very different than php. See this article : https://www.mql5.com/en/articles/567

Hello and thanks for answer.

I already read that article, but I can't understand how to insert the elements inside the loop.

I tried the ArrayFill, but without success. In fact I suppose that that function doesn't work with string :(

 
somtam:

Hello and thanks for answer.

I already read that article, but I can't understand how to insert the elements inside the loop.

I tried the ArrayFill, but without success. In fact I suppose that that function doesn't work with string :(

string firstArr[7];
int size=ArraySize(currency);

for(int i=0,j=0; i<size; i++)
  {

   if(StringFind(currency[i],firstCurrency,0)!=-1)
     {

      // push the element into the first available index of firstArr
      // like in php i do with
      firstArr[j]=currency[i];
      j++
     }

  }
I let you manage j variable to avoid array out of range.
 

Hello, and thanks a lot for helping,

I add a dynamic array and a checker for the error.

string firstArr[];
int size = ArraySize(currency);
for( int i=0, j=0; i < size; i++ ){
   
   if( StringFind(currency[i], firstCurrency, 0) != -1 ){
     ArrayResize(firstArr,j+1);
     firstArr[j]=currency[i];
     j++;
   }
}
   
int errCode = GetLastError();
string errDesc = ErrorDescription(errCode);
Alert( StringConcatenate("Error code: ", errCode," - Error Desc: ", errDesc))

With this code everything is seems to work. But if, on purpose to check the getLastError, I comment the line of ArrayResize, it doesn't print the string with the error. There is only the Array Out Of Range in the Expert Panel. 

But the most difficult point to understand it will be how to create a function like:

firstArr = get_symbols_array( currency, firstCurrency );

where this function makes the job that we have inside the for cycle. 

sorry about that, I am a completely newbie and this point looks very different from php.

thanks again! 

 
somtam:

Hello, and thanks a lot for helping,

I add a dynamic array and a checker for the error.

With this code everything is seems to work. But if, on purpose to check the getLastError, I comment the line of ArrayResize, it doesn't print the string with the error. There is only the Array Out Of Range in the Expert Panel.

Yes "Array out of range" is a fatal error, you can't catch it.


But the most difficult point to understand it will be how to create a function like:

firstArr = get_symbols_array( currency, firstCurrency );

where this function makes the job that we have inside the for cycle. 

sorry about that, I am a completely newbie and this point looks very different from php.

thanks again! 

You can't return an array with a function. You have to pass your array by reference to your function. You can also use OOP techniques, but it's an other matter.

get_symbols_array( currency, firstCurrency, firstArr );

and

void get_symbols_array(string &currency[],string firstCurrency, string &firstArr[]);
 

But if I pass firstArr like a reference, does it means that I can change the values only for that array?

So another call like this:

get_symbols_array( currency, secondCurrency, secondArr );

is not possible, right?

Moreover I get this error, probably because the declaration of currency is at the beginning of the code. 

declaration of 'currency' hides global declaration at line 15

thank you very much for helping!

 
somtam:

But if I pass firstArr like a reference, does it means that I can change the values only for that array?

So another call like this:

get_symbols_array( currency, secondCurrency, secondArr );

is not possible, right?

Yes it's possible, you can use any array of course, otherwise functions would be useless.

get_symbols_array( currency, firstCurrency, firstArr );

and

void get_symbols_array(string &anyCurrency[],string anyCurrency, string &anyArr[]);

Moreover I get this error, probably because the declaration of currency is at the beginning of the code. 

declaration of 'currency' hides global declaration at line 15

thank you very much for helping!

It's not an error, it's a warning, as you are using the same name for local variable and global variable.
 

Hey angevoyageur! you are the best!

everything is working now!

It is a little bit strange for me that I have to be careful about the name I write as a parameter in the function. Usually I am free to use whatever I like.

But I see that with mql ( c++ ) I have to really understand more deeply the concept of initialization, memory allocation and more...

Thanks again! 

Reason: