Array Invalid Index Value

 

This mql5 code is giving error as shown. Why and how to fix it?

"[" invalid index value

Thanks

template<typename T>
uint arrayUnshift(T &arr[], T val){
  uint newSize = ArraySize(arr) + 1;
  T newArray[newSize];  //double click on the line in the metaeditor description moves the cursor to the end of the "newArray" 
  newArray[0] = val;
  ArrayCopy(newArray, arr, 1, 0);
  return ArraySize(newArray);
}
 
samjesse:

This mql5 code is giving error as shown. Why and how to fix it?

"[" invalid index value

Thanks

Try something like this:

template<typename T>
uint arrayUnshift(T &arr[], T val){
  uint newSize = ArraySize(arr) + 1;
  T newArray[];  //double click on the line in the metaeditor description moves the cursor to the end of the "newArray" 
  ArrayResize(newArray,newSize,0);  
  newArray[0] = val;
  ArrayCopy(newArray, arr, 1, 0);
  return ArraySize(newArray);
}
 

I cann't get the unShift function to work. I need to be able to add an item to the beginning of the array.

string arr[2];
arr[0]="b";
arr[1]="c";
arrayPrint(arr);
arrayUnshift(arr,"a");
arrayPrint(arr);

template<typename T>
uint arrayUnshift(T &arr[], T val){
  uint newSize = ArraySize(arr) + 1;
  T newArray[];
  ArrayResize(newArray,newSize,0);  
  newArray[0] = val;
  ArrayCopy(newArray, arr, 1, 0);
  return ArraySize(newArray);
}
template<typename T> 
void arrayPrint(T &arr[]){
  Print("print array of size " + ArraySize(arr));
  for(int i = 0; i < ArraySize(arr); i++){
    Print("array element " + i + " =", arr[i]);
  }
}  

/*
OUTPUT:
print array of size 2
array element 0 = b
array element 1 = c
for both the print statements

*/

I expected to get the new array of 3 elements to have a,b,c in it.

 
samjesse:

I cann't get the unShift function to work. I need to be able to add an item to the beginning of the array.


I expected to get the new array of 3 elements to have a,b,c in it.

Ok. I think the only thing missing is copying back from newArray to arr before exiting the method.

 
Andrey Barinov:

Ok. I think the only thing missing is copying back from newArray to arr before exiting the method.

Humm...

Isn't the below "included" line what does that?

ArrayCopy(newArray, arr, 1, 0);
 
samjesse:

Humm...

Isn't the below "included" line what does that?

this is copying from the original array to the temp array. After that you do some transformation with the temp array and should copy it back to the original array.

 
Andrey Barinov:

this is copying from the original array to the temp array. After that you do some transformation with the temp array and should copy it back to the original array.

OK. I tried but failed to fix it. I tried to go another way as shown the the code below but see the fail of the ArrayResize why?

template<typename T>
uint arrayUnshift(T &arr[], T val){
  uint newSize = ArraySize(arr) + 1;
  T tempElement = arr[0];
  arr[0] = val;

  int res = ArrayResize(arr, newSize);
  Print("size operation: ", res); // prints -1. why did the opeation fail to resize the array???
  for(int i = 1; i < newSize; i++){
    T item = arr[i];
    arr[i] = tempElement;
    Print(i," ",arr[i]);
    tempElement = item;
  }
  // T tempArr[];
  // ArrayResize(tempArr,newSize,0);  
  // tempArr[0] = val;
  // ArrayCopy(tempArr, arr, 1, 0);
  // ArrayCopy(arr, tempArr);
  return ArraySize(arr);
}
 
samjesse:

OK. I tried but failed to fix it. I tried to go another way as shown the the code below but see the fail of the ArrayResize why?

template<typename T>
uint arrayUnshift(T &arr[],T val)
  {
   uint newSize=ArraySize(arr)+1;
   T newArray[];
   ArrayResize(newArray,newSize);
   ArrayCopy(newArray,arr,1,0);
   newArray[0]=val;
   ArrayCopy(arr,newArray,0,0);
   return ArraySize(newArray);
  }

or

template<typename T>
uint arrayUnshift(T &arr[],T val)
  {
   ArraySetAsSeries(arr,true);
   uint size=ArraySize(arr);
   ArrayResize(arr,size+1);
   arr[size]=val;
   ArraySetAsSeries(arr,false);
   return(size+1);
  }
 
Alain Verleyen:

or

template<typename T>
uint arrayUnshift(T &arr[],T val)
  {
   uint newSize=ArraySize(arr)+1;
   T newArray[];
   ArrayResize(newArray,newSize);
   ArrayCopy(newArray,arr,1,0);
   newArray[0]=val;
   Print(ArraySize(newArray)); //<<<<<< PRINTS 3
   int res = ArrayCopy(arr,newArray,0,0);
   Print("res: ", res);      //<<<<<< PRINTS "res: 2"  Why this problem?, 3 is expected.
   return ArraySize(newArray);
  }
Reason: