Is there a better way to do this.
The built in function ArrayInitialize() does not work with string arrays.
- It doesn't? What gives you that idea?
- A variable array tells you exactly how many lines have been set.
string MultiClose[]; int iMsg = ArraySize(MultiClose); ArrayResize(MultiClose, iMsg+1); MultiClose[iMsg] = "AA"; iMsg = ArraySize(MultiClose); ArrayResize(MultiClose, iMsg+1); MultiClose[iMsg] = "BB"; : int nMsg = ArraySize(MultiClose); for (iMsg = 0; iMsg < nMsg; iMsg++) Print(MultiClose[iMsg]);
- If you know the maximum number of lines then you don't even need a variable array, just initialize the count to zero at the beginning
#define MC_MAX 99 string MultiClose[MC_MAX]; nMsg = 0; MultiClose[nMsg] = "AA"; nMsg++; MultiClose[nMsg] = "BB"; nMsg++; : for (iMsg = 0; iMsg < nMsg; iMsg++) Print(MultiClose[iMsg]);
- It doesn't? What gives you that idea?
- A variable array tells you exactly how many lines have been set.
- If you know the maximum number of lines then you don't even need a variable array, just initialize the count to zero at the beginning
-- 1
string Array1[] = {"AA","BB"}; int i = ArraySize(Array1); Print("Size before ArraryInitialize should be 2 and ",i," is the value, True"); Print("Array1[0] = ",Array1[0]); Print("Array1[1] = ",Array1[1]); ArrayInitialize(Array1, ""); i = ArraySize(Array1); Print("ArraySize after ArraryInitialize should be = 0 and ",i," is the value, False"); Print("Array1[0] = ",Array1[0]); Print("Array1[1] = ",Array1[1]);
This is the runtime output from the sample code I above, I think I have the ArrayInitialize() coded correctly, It does compile. Although I would have expected the compiler to to pick up on the datatype mismatch in the function at compile time.
2011.09.14 14:23:52 Invictus_v2.3 EURJPY,H1: Size before ArraryInitialize should be 2 and 2 is the value, True
2011.09.14 14:23:51 Invictus_v2.3 EURJPY,H1: Array1[1] = BB
2011.09.14 14:23:51 Invictus_v2.3 EURJPY,H1: Array1[0] = AA
2011.09.14 14:23:51 Invictus_v2.3 EURJPY,H1: ArraySize after ArraryInitialize should be = 0 and 2 is the value, False
2011.09.14 14:23:51 Invictus_v2.3 EURJPY,H1: ArrayInitialize function does not process string arrays
2011.09.14 14:23:51 Invictus_v2.3 EURJPY,H1: Array1[1] = BB
2011.09.14 14:23:51 Invictus_v2.3 EURJPY,H1: Array1[0] = AA
--2 I have a local array[] in a function that is going to get called many times. the array may or may not get filled. If it does not, that is fine, I can test array[0] to STRING_EMPTY. But once it is populated I need to set it back to STRING_EMPTY the next time in. Whatever values were in the array last time will still be in it. Arrays are static, and I have several arrays in different functions, thats why I wanted to make a function, or use a built in function to set it STRING_EMPTY at every entry into the function. I wanted to use a builtin Array function or a combo of them because they are usually optimized for speed.
--3 Dynamic, I don't know the size.
2011.09.14 14:23:51 Invictus_v2.3 EURJPY,H1: ArraySize after ArraryInitialize should be = 0 and 2 is the value, False
ArrayInitialize only works on double arrays. It does NOT modify their size. It sets every element to a constant.
ArrayResize modifies array sizes.
WRONG!
ArrayInitialize only works on double arrays. It does NOT modify their size. It sets every element to a constant.
ArrayResize modifies array sizes.
"It doesn't? What gives you that idea? " You did, unless I misread your prev post. The my last post was just an example to show the runtime error on ArrayInitialize() with a string array. I said in my first post that it did not work with a string array, although my print statements were dopy.
We are talking about 2 different things. In my original post I had an array with 2 elements, AA and BB. I never resized the array, I don't care about the size or if I need to change the size? I posted a function that does the same for string arrays that ArrayInitalize() does for doubles, set all the elements to a specific value. In my case "". I was just asking if there were a combination of built in array functions that are already provided that I may have missed in the doc that would give me the same abilty as what I wrote. From everything I have read there does not seem to any combination of builtin array functions to accomplish a pretty simple task.
sorry for the confusion.
the function returns a value so should be a string. code not tested though.
#define ARRAY_SIZE 4 #define _STR_EMPTY "" #define _STR_DEFAULT "default" extern string string_a = "AA", string_b = "BB", string_c = "", string_d; string array_string[]; int i; int init() { //---- indicators StringArrayInitialize(array_string); PrintStringArray(array_string); //---- return(0); } string StringArrayInitialize(string& arraystr[]) { ArrayResize(arraystr,ARRAY_SIZE); arraystr[0] = string_a; arraystr[1] = string_b; arraystr[2] = string_c; arraystr[3] = string_d; for(i=0;i<ARRAY_SIZE;i++) { if(arraystr[i] != _STR_EMPTY) { arraystr[i] = _STR_EMPTY; } else { arraystr[i] = _STR_DEFAULT; } return(arraystr); } } void PrintStringArray(string strarray[]) { int element=i+1; for(i=0;i<ArraySize(strarray);i++) { string _message_a = "string_array element "+ element + " initialized to "+ _STR_EMPTY; string _message_b = "string_array element "+ element + " set to "+ _STR_DEFAULT; if(strarray[i] != _STR_EMPTY) { Print(_message_a); element++;} else { Print(_message_b); element++;} } }
"It doesn't? What gives you that idea? " You did, unless I misread your prev post. T
which is patently false. The array size after initialize is always the same as before.
I misread your original post as since you then you said
which is patently false. The array size after initialize is always the same as before.
-- 1
This is the runtime output from the sample code I above, I think I have the ArrayInitialize() coded correctly, It does compile. Although I would have expected the compiler to to pick up on the datatype mismatch in the function at compile time.
2011.09.14 14:23:52 Invictus_v2.3 EURJPY,H1: Size before ArraryInitialize should be 2 and 2 is the value, True
2011.09.14 14:23:51 Invictus_v2.3 EURJPY,H1: Array1[1] = BB
2011.09.14 14:23:51 Invictus_v2.3 EURJPY,H1: Array1[0] = AA
2011.09.14 14:23:51 Invictus_v2.3 EURJPY,H1: ArraySize after ArraryInitialize should be = 0 and 2 is the value, False
2011.09.14 14:23:51 Invictus_v2.3 EURJPY,H1: ArrayInitialize function does not process string arrays
2011.09.14 14:23:51 Invictus_v2.3 EURJPY,H1: Array1[1] = BB
2011.09.14 14:23:51 Invictus_v2.3 EURJPY,H1: Array1[0] = AA
--2 I have a local array[] in a function that is going to get called many times. the array may or may not get filled. If it does not, that is fine, I can test array[0] to STRING_EMPTY. But once it is populated I need to set it back to STRING_EMPTY the next time in. Whatever values were in the array last time will still be in it. Arrays are static, and I have several arrays in different functions, thats why I wanted to make a function, or use a built in function to set it STRING_EMPTY at every entry into the function. I wanted to use a builtin Array function or a combo of them because they are usually optimized for speed.
--3 Dynamic, I don't know the size.
try this classic method
for(i=0; i<2; i++) Array1[i]="";
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Is there a better way to do this. The built in function ArrayInitialize() does not work with string arrays. I want to be able to test a string array to see if its elements have been populated with anything other than an empty string. I also want to initialize the array, since they are static, at each entry into a funtion so I dont pass old data my email functions. Here is a sample of what I have come up with. The code below returns true, seems like alot of work