Download MetaTrader 5

fill array with variables

To add comments, please log in or register
florenceale
136
florenceale  

Hi one question...

every time i try to fill an array with variables it returns this mistake  "constant expression required"

this is the code:

 string tab_ordini[8] = { ord_a, ord_a_reverse, ord_b, ord_b_reverse, ord_a_s, ord_a_s_reverse, ord_b_s, ord_b_s_reverse };

and i have declared them in this way. They are inside a function


string ord_a = StrToInteger(ObjectDescription("ord_a"));
string ord_a_reverse = StrToInteger(ObjectDescription("ord_a_reverse"));
string ord_b = StrToInteger(ObjectDescription("ord_b"));
string ord_b_reverse = StrToInteger(ObjectDescription("ord_b_reverse"));
string ord_a_s = StrToInteger(ObjectDescription("ord_a_s"));
string ord_a_s_reverse = StrToInteger(ObjectDescription("ord_a_s_reverse"));
string ord_b_s = StrToInteger(ObjectDescription("ord_b_s"));
string ord_b_s_reverse = StrToInteger(ObjectDescription("ord_b_s_reverse"));

Is there a way to do with just one expression, or i have to specify every item of the array?

Fernando Carreiro
3951
Fernando Carreiro  
florenceale:

Hi one question...

every time i try to fill an array with variables it returns this mistake  "constant expression required"

this is the code:

and i have declared them in this way. They are inside a function

Is there a way to do with just one expression, or i have to specify every item of the array?

You can't initialise an array with "variables", only with "constant" data!

florenceale
136
florenceale  

Hi, I have another question,

I have noticed that ArrayBsearch it works fine just with number series...like this

  int tab_ordini[9];
   tab_ordini[0] = 1124;
   tab_ordini[1] = 1125;
   tab_ordini[2] = 1126;
   tab_ordini[3] = 1127;
   tab_ordini[4] = 1128;
   tab_ordini[5] = 1129;
   tab_ordini[6] = 1130;
   tab_ordini[7] = 1131;
   tab_ordini[8] = 1132;
  
   
   int trova_valore =  ArrayBsearch(tab_ordini,"1124",WHOLE_ARRAY,0, MODE_ASCEND);
   


but if I have some random numbers included 0, is not returning an index i can use in some way. 

I would need like this. And the numbers in most of case will be 0. 

What Im try to do to explain it better...i have 9 fields. I want to know if there is one with the number I'm looking for...with this ArrayBsearch I can't achieve what I want...

  int tab_ordini[9];
   tab_ordini[0] = 0;
   tab_ordini[1] = 1125;
   tab_ordini[2] = 0;
   tab_ordini[3] = 0;
   tab_ordini[4] = 0;
   tab_ordini[5] = 1129;
   tab_ordini[6] = 0;
   tab_ordini[7] = 0;
   tab_ordini[8] = 0;
  
   
   int trova_valore =  ArrayBsearch(tab_ordini,"1125",WHOLE_ARRAY,0, MODE_ASCEND);
whroeder1
17050
whroeder1  
florenceale: his ArrayBsearch I can't achieve what I want...

Because, as the documentation states, the array must be sorted first.

florenceale
136
florenceale  

sorry but i'm not understanding....

I thought it was already sorted in this way after I have changed this

 string tab_ordini[8] = { ord_a, ord_a_reverse, ord_b, ord_b_reverse, ord_a_s, ord_a_s_reverse, ord_b_s, ord_b_s_reverse };

 into this:

   int ord_a = StrToInteger(ObjectDescription("ord_a"));
   int ord_a_reverse = StrToInteger(ObjectDescription("ord_a_reverse"));
   int ord_b = StrToInteger(ObjectDescription("ord_b"));
   int ord_b_reverse = StrToInteger(ObjectDescription("ord_b_reverse"));
   int ord_a_s = StrToInteger(ObjectDescription("ord_a_s"));
   int ord_a_s_reverse = StrToInteger(ObjectDescription("ord_a_s_reverse"));
   int ord_b_s = StrToInteger(ObjectDescription("ord_b_s"));
   int ord_b_s_reverse = StrToInteger(ObjectDescription("ord_b_s_reverse"));
   
   int tab_ordini[9];
   tab_ordini[0] = ord_a;
   tab_ordini[1] = ord_a_reverse;
   tab_ordini[2] = ord_b;
   tab_ordini[3] = ord_b_reverse;
   tab_ordini[4] = ord_a_s;
   tab_ordini[5] = ord_a_reverse;
   ...
  
   
   int trova_valore =  ArrayBsearch(tab_ordini,ticket_number,WHOLE_ARRAY,0, MODE_ASCEND);
whroeder1
17050
whroeder1  
florenceale: sorry but i'm not understanding....

I thought it was already sorted in this way after I have changed this

  int tab_ordini[9];
   tab_ordini[0] = 0;
   tab_ordini[1] = 1125;
   tab_ordini[2] = 0;
   tab_ordini[3] = 0;
   tab_ordini[4] = 0;
   tab_ordini[5] = 1129;
   tab_ordini[6] = 0;
   tab_ordini[7] = 0;
   tab_ordini[8] = 0;

Is "0 1125 0 0 0 1129 0 0 0" in sorted order, or is "0 0 0 0 0 0 0 1125 1129?" What part of

Note Binary search processes only sorted arrays. To sort numeric arrays use the ArraySort() function.
          ArrayBsearch - Array Functions - MQL4 Reference
is unclear?
florenceale
136
florenceale  

ok now perfect. I hadn't found the other function, and read that row. Thanks

To add comments, please log in or register