Array insert new value at [0] location

 
I'm trying to insert to an exsisting array new values so it will be in an order.
I want to insert new int value in the [0] location and that the exsisting [0] location will be moved to [1] etc...

I went though the docs and i havent found a way to do this..

Does anyone know of a way? or have a code that he can share for it?
Cuz i dont want to have a for loop..
 

Hi , when inserting one element you can refer to the code below otherwise (when insertign an array to an array) use https://www.mql5.com/en/docs/array/arrayinsert

First you resize the Array , and you add one element , you take the existing size and add one :

ArrayResize(list,ArraySize(list)+1,0);

Then you self copy , it works , with ArrayCopy .You copy all the elements of the array -1 starting from position 1 on the destination array but only if the ArraySize is >1

if(ArraySize(list)>1){
ArrayCopy(list,list,1,0,ArraySize(list)-1);
}

Then you can insert your new element in slot [0] of the list

Documentation on MQL5: Array Functions / ArrayInsert
Documentation on MQL5: Array Functions / ArrayInsert
  • www.mql5.com
ArrayInsert - Array Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Lorentzos Roussos #:

Hi , when inserting one element you can refer to the code below otherwise (when insertign an array to an array) use https://www.mql5.com/en/docs/array/arrayinsert

First you resize the Array , and you add one element , you take the existing size and add one :

Then you self copy , it works , with ArrayCopy .You copy all the elements of the array -1 starting from position 1 on the destination array but only if the ArraySize is >1

Then you can insert your new element in slot [0] of the list

Thank you!

 
Lorentzos Roussos #:

Hi , when inserting one element you can refer to the code below otherwise (when insertign an array to an array) use https://www.mql5.com/en/docs/array/arrayinsert

First you resize the Array , and you add one element , you take the existing size and add one :

Then you self copy , it works , with ArrayCopy .You copy all the elements of the array -1 starting from position 1 on the destination array but only if the ArraySize is >1

Then you can insert your new element in slot [0] of the list

Am I the only one doing this?😄

int arrSize = ArraySize(trends);
if(!resize(arrSize - 1)                    // Remove trends[oneLessThanArrSize]
|| !setAsSeries(false) || !resize(arrSize) // Add trends[0]
|| !setAsSeries(true))                     // Set AS_SERIES back to true
   return(false);
trends[0].write(...);

Above is a piece that I copied from real code. I think the meaning should be clear.

This example assumes the array has AS_SERIES = true. resize() and setAsSeries() are wrapper methods for ArrayResize and ArraySetAsSeries.

I will later make and post here an example code that can be run


[EDITED]

I didn't understand the task correctly. The code snippet above adds a new value and removes a value from the opposite end of the array. That is, the array size remains the same

 
Vladislav Boyko #:

Am I the only one doing this?😄

Above is a piece that I copied from real code. I think the meaning should be clear.

This example assumes the array has AS_SERIES = true. resize() and setAsSeries() are wrapper methods for ArrayResize and ArraySetAsSeries.

I will later make and post here an example code that can be run


[EDITED]

I didn't understand the task correctly. The code snippet above adds a new value and removes a value from the opposite end of the array. That is, the array size remains the same

You are using a ring buffer 

 
Lorentzos Roussos #:

You are using a ring buffer 

I just googled what a ring buffer is. Yes, it's very similar to a ring buffer.

I came up with this when I didn't want to copy the elements of a struct array.

Now I will know about ring buffer, thank you :)

Reason: