Initializing a string Array[]

 

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

#define STRING_EMPTY ""

void myArrayInit(string & Array[]){
   for(int j=0; j<ArraySize(Array); j++)
      Array[j]=STRING_EMPTY;
   return;   
}
int start()
{  
 // string[] test
   string MultiClose[]= {"AA","BB"}; 
    
   myArrayInit(MultiClose); 
   if (MultiClose[0]==STRING_EMPTY)
      Print("Array is Empty = true");
   else 
     Print("Array is Empty = false");
 
danjp:
Is there a better way to do this.
The built in function ArrayInitialize() does not work with string arrays.
  1. It doesn't? What gives you that idea?
  2. 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]);
  3. 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]);

 
WHRoeder:
  1. It doesn't? What gives you that idea?
  2. A variable array tells you exactly how many lines have been set.
  3. 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.

 
danjp:
2011.09.14 14:23:51 Invictus_v2.3 EURJPY,H1: ArraySize after ArraryInitialize should be = 0 and 2 is the value, False
WRONG!
ArrayInitialize only works on double arrays. It does NOT modify their size. It sets every element to a constant.

ArrayResize modifies array sizes.

 
WHRoeder:
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++;}
    }
  }  
 
danjp:
"It doesn't? What gives you that idea? " You did, unless I misread your prev post. T
I misread your original post as
ArrayInitialize() ArrayResize() does not work with string arrays.
since you then you said
arraySize after ArraryInitialize should be = 0 and 2 is the value, False

which is patently false. The array size after initialize is always the same as before.

 
WHRoeder:
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.


My bad, I started out testing something else and did not go back and delete the the print statements which made everything even more confusing. Being new to this lang I have used your WHRea.mq4 to double check my work and I also use the 4/5 digit broker code.
 
danjp:


-- 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]="";