How to get access to array in loop with variable

 

Hello!

In a loop I want to assaign the values of the operator to specific arrays depending on the loop-variable.

This is a simplified example: 

---------------------------------------------------------------  

double Array1[1], Array2[1], Array3[1], Array4[1],...; 

for (int i=0;i<=100;i++)

{ double HighX=High[i];

  if (i=1) Array1[0]=HighX;                                         //this list I want to avoid

  if (i=2) Array2[0]=HighX;

  if (i=3) Array3[0]=HighX;

  ...

--------------------------------------------------------------- 

double ARRAYname= "Array"+i;                                 //if  (i==1)  ARRAYname=Array1;

ARRAYname[0]=HighX;                                             //this doesn´t give access to the "i"-specific Array. 

---------------------------------------------------------------

---------------------------------------------------------------  

Is there any solution to get the specific array using a combination of "Array" and "i"?

 

hello,

instead of Array1[], Array2[] and so on, you can define a 2 dimensional array, for example, Array[i][k].

 best regards

 
Demos:

hello,

instead of Array1[], Array2[] and so on, you can define a 2 dimensional array, for example, Array[i][k].

 best regards

Thanks Demos for responding!

In reality I will need a 2 dimensional "Array[1000][1]" for "Time" (dim1) and "Price" (dim2), that afterwards I can sort by "Time".

Can I use  a 3 dimensional array "Array[1000][1][i]", or by sorting the array will be desorganized, so that I cannot get again right acccess to "i" in the 3. dimension?

It´s very dfficult to understand how works a 3 dimensional array, the documentation about it is not very good. 

The most transparent way to get it would be to change the name of the array "Arrayi[][]", if there is any possibility to do this.

Thanks! 

 

You can but why are you so concerned with "i", you can use anything to iterate through your loops and if your clever you can find ways of turning big loops or lots of code into smaller portions.

 
kreisel:

Thanks Demos for responding!

In reality I will need a 2 dimensional "Array[1000][1]" for "Time" (dim1) and "Price" (dim2), that afterwards I can sort by "Time".

Can I use  a 3 dimensional array "Array[1000][1][i]", or by sorting the array will be desorganized, so that I cannot get again right acccess to "i" in the 3. dimension?

It´s very dfficult to understand how works a 3 dimensional array, the documentation about it is not very good. 

The most transparent way to get it would be to change the name of the array "Arrayi[][]", if there is any possibility to do this.

Thanks! 

hey, no problem, my pleasure :) On to the topic, I must say that fiddling with multi dimensional arrays, is a sure way to go crazy :) Regarding your question, from what I can understand , you want to use the third dimension as a label, an index. But I do not believe that you do need to do that; in your specific case, I guess you can use the first dimension, which contains the values for Time, as index.

If you want to use ArraySort(), make sure that you do understand how it is working; for example, you are writing

double ARRAYname= "Array"+i;  

 althought you were most likely wanting to write string ARRAYname. As it is, your "Array"+i is converted to a double number (or zero, as you will see if you try to print the result of the cast). For example, 

double a='b';    /* it will print the ASCII value of b which is 98 */

If the documentation is not enough (which is the case many times , not only in MQL but in other languages as well) you have to make your own experiments to (possibly) found out how things are working.

So, for the said function (ArraySort() ), on a e.g. 2d array, I can realise that it moves all elements in the 2nd dimension according to to the result of the sorting, which happens on the 1st dimension . If you want some other sorting, you may have to write your own sorting algorithm 

Regarding 3d arrays, well, what to say here :) A possible issue would be on how to fill an array like that; i could come up with a way that can probably show how arrays are indexed internally in MQL; have a look

 

char array[3][3][3]={ { {1,2},{3,4,5},{6,7,8} },{ {9,10,11},{12,13,14},{15,16,17} },
                         { {18,19,20},{21,22,23},{24,25,26} } };

 

For example, the first group is like 0,0{0,1,2}, 0,1{0,1,2}, 0,2{0,1,2} ; numbers represent index; but as I said, I do not believe that you need a 3d array for what you are after

P.S. the firrst group {1,2} is not a typo; MQL fills the blanc with zero so that the indexing of the groups after is correct. This shows that not every array member must have the same number of sub-members. For example, consider array[3][3]; array[0] may have three members but array[1] can have  one member and array[2] two

0 1 2
1   1
2

 

best regards 

 
Demos:

hey, no problem, my pleasure :) On to the topic, I must say that fiddling with multi dimensional arrays, is a sure way to go crazy :) Regarding your question, from what I can understand , you want to use the third dimension as a label, an index. But I do not believe that you do need to do that; in your specific case, I guess you can use the first dimension, which contains the values for Time, as index.

If you want to use ArraySort(), make sure that you do understand how it is working; for example, you are writing

 althought you were most likely wanting to write string ARRAYname. As it is, your "Array"+i is converted to a double number (or zero, as you will see if you try to print the result of the cast). For example, 

If the documentation is not enough (which is the case many times , not only in MQL but in other languages as well) you have to make your own experiments to (possibly) found out how things are working.

So, for the said function (ArraySort() ), on a e.g. 2d array, I can realise that it moves all elements in the 2nd dimension according to to the result of the sorting, which happens on the 1st dimension . If you want some other sorting, you may have to write your own sorting algorithm 

Regarding 3d arrays, well, what to say here :) A possible issue would be on how to fill an array like that; i could come up with a way that can probably show how arrays are indexed internally in MQL; have a look

 

 

For example, the first group is like 0,0{0,1,2}, 0,1{0,1,2}, 0,2{0,1,2} ; numbers represent index; but as I said, I do not believe that you need a 3d array for what you are after

P.S. the firrst group {1,2} is not a typo; MQL fills the blanc with zero so that the indexing of the groups after is correct. This shows that not every array member must have the same number of sub-members. For example, consider array[3][3]; array[0] may have three members but array[1] can have  one member and array[2] two

 

best regards 

Thanks Demos!

I think I understand what I need and how arrays are working.

A 3 dim array is like a sliced loaf of bread and the numbers of the slices are the index of dim1. So I can assign to "Time" in the 1.dim (array[x][0][0]), depending on the first condition (number "y" of dim2, array[x][y][0]), the value of the second condition ("z" of dim3) in position array[x][y][z]. By sorting only the slices are sorted. So afterwards, depending on dim1, I can get access to the value array[x][y][z] depending on dim2 and dim3.

I only wonder if I will get a problem of memory if I´m using 40, 60 or 80 arrays[1000][150][150] in 30 different currencies at the same time.... (??)

 
kreisel:

a) In reality I will need a 2 dimensional "Array[1000][1]" for "Time" (dim1) and "Price" (dim2), that afterwards I can sort by "Time".

kreisel:

b) A 3 dim array is like a sliced loaf of bread and the numbers of the slices are the index of dim1. By sorting only the slices are sorted. So afterwards, depending on dim1, I can get access to the value array[x][y][z] depending on dim2 and dim3.

c) I only wonder if I will get a problem of memory if I´m using 40, 60 or 80 arrays[1000][150][150] in 30 different currencies at the same time.... (??)

 hello ,

a) We may need some more insight of what you are planning to do, as I am not really sure what is going on. For example, if dim1 contains Time, normally your array will be already sorted as it is getting filled by time; unless something has changed, time is going in one direction and let me tell you, I am not a fan of Einstein regarding that matter :)

Nevertheless, I can tell you, I am still quite not "convinced" you do need the 3d dimension; I have the impression that you need the "variable" variables as a reference somewhere. But you can, instead of, e.g., referring to Array5[dim2][dim3], refer to Array[dim1][dim2] and use the index of dim1 as your guide. But as I said, we may have to explain  some more about your goal

b) nice analogy :)

c) A couple of technical remarks now. From (a) you are saying you will have Time in dim1 and Price in dim2. Your best bet could be to have your array as long, as time is 8 bytes long. In that case also, you will have to do something such as Price*100000 to get the price expressed in integer (assuming a 5 digits part). As for the size I believe you are right to be concerned, as the example you are ginving is already getting you to around 180MB. 


best regards 


 
Demos:

 hello ,

a) We may need some more insight of what you are planning to do, as I am not really sure what is going on. For example, if dim1 contains Time, normally your array will be already sorted as it is getting filled by time; unless something has changed, time is going in one direction and let me tell you, I am not a fan of Einstein regarding that matter :)

Nevertheless, I can tell you, I am still quite not "convinced" you do need the 3d dimension; I have the impression that you need the "variable" variables as a reference somewhere. But you can, instead of, e.g., referring to Array5[dim2][dim3], refer to Array[dim1][dim2] and use the index of dim1 as your guide. But as I said, we may have to explain  some more about your goal

b) nice analogy :)

c) A couple of technical remarks now. From (a) you are saying you will have Time in dim1 and Price in dim2. Your best bet could be to have your array as long, as time is 8 bytes long. In that case also, you will have to do something such as Price*100000 to get the price expressed in integer (assuming a 5 digits part). As for the size I believe you are right to be concerned, as the example you are ginving is already getting you to around 180MB. 


best regards 


Thanks Demos!

My thinking was about  "double array[x][y][z]". Generally speaking I want to save 3 variables. Index of Dim2 is variable1, Index of Dim3 is variable2, the value saved in array[x][var1][var2] in points is a presentation of the 3.variable, which I can use for further calculations.

Yes, "time in sec since 1970" should be already in the right order. But also I need to save time as "TimeHour+TimeMinute". One I can save in array[x][0][0], the other one in array[x][1][0]. For sorting it´s very easy to replace them with the aid of array[x][2][0].

about c): because "time in sec since 1970" has only 10 digits (iTime(NULL,1440,0) from today: 1 450 742 400) and point is 0.00001 wouldn´t it better to use "float array[][][]"? Or do You think using (int array[][][]) with converting of "points" would be the better alternative? "long" also uses 8 bytes like "double".

The size of the int type is 4 bytes (32 bits). The minimal value is -2 147 483 648, the maximal one is 2 147 483 647.

The size of the long type is 8 bytes (64 bits). The minimum value is -9 223 372 036 854 775 808, the maximum value is 9 223 372 036 854 775 807.  

Type

Size in bytes

Minimal Positive Value

Maximum Value

C++ Analog

float

4

1.175494351e-38

3.402823466e+38

float

double

8

2.2250738585072014e-308

1.7976931348623158e+308

double

 But "array[1000][150][150]" seems only to be so large. E.g. variable1 shall be 10,15,20,25,...etc. Because I cannot use half array-indexes as e.g. "1.5" (variable1/10) there will be many empty spaces with value "0" (e.g. the space between array[x][10][] and array[x][15][]). So I´m thinking it could be better to divide the array to avoid these empty spaces. Or it could be better, after finishing the calculation, just to delete the values by using "ArrayResize(array,0)". By testing I will see what will be the best.

 

Thanks so much!

Kind regards 

 

My thinking was about  "double array[x][y][z]". Generally speaking I want to save 3 variables. Index of Dim2 is variable1, Index of Dim3 is variable2, the value saved in array[x][var1][var2] in points is a presentation of the 3.variable, which I can use for further calculations.

Yes, "time in sec since 1970" should be already in the right order. But also I need to save time as "TimeHour+TimeMinute". One I can save in array[x][0][0], the other one in array[x][1][0]. For sorting it´s very easy to replace them with the aid of array[x][2][0].

about c): because "time in sec since 1970" has only 10 digits (iTime(NULL,1440,0) from today: 1 450 742 400) and point is 0.00001 wouldn´t it better to use "float array[][][]"? Or do You think using (int array[][][]) with converting of "points" would be the better alternative? "long" also uses 8 bytes like "double".

 Concerning the use of doubles or longs, I assume you can use double , also for time , but not float , in order not to get an unnecessary cast from price (double) to float.

 Or, you can split to seperate arrays and gain some speed avoiding the cast from datetime to double ; 


 But "array[1000][150][150]" seems only to be so large. E.g. variable1 shall be 10,15,20,25,...etc. Because I cannot use half array-indexes as e.g. "1.5" (variable1/10) there will be many empty spaces with value "0" (e.g. the space between array[x][10][] and array[x][15][]). So I´m thinking it could be better to divide the array to avoid these empty spaces. Or it could be better, after finishing the calculation, just to delete the values by using "ArrayResize(array,0)". By testing I will see what will be the best.

I am pretty sure for the fact that you do not need to have array[1000][150][150] ; When you have e.g. array[1000][3], as you have said before, at [x][0] you can store hours, at [x][1] you can store corresponding minutes , at [x][2] corresponding price and so on (for example, you can have array[1000][11] and from [x][3] to [x][10] you can store a 8 character string - but that looks like a waste of space and you could be better of using a seperate char array[1000][8] )

  - as a sidenote, ArraySort() will sort [x][1] and array[x][2] in accordance to values present in [x][0] so be cautious - 


best regards 

Reason: