array_copy

Questions about MultiCharts and user contributed studies.
BD.
Posts: 19
Joined: 02 Feb 2024
Has thanked: 2 times
Been thanked: 2 times

array_copy

Postby BD. » 08 Feb 2024

How does array_copy work? I'm trying to shift elements of an array using "array_copy" description clearly states that it should copy "beginning at the index of N"(see below) instead it copies the element of index N for a specified amount.
Issues start to appear when I enter "NumberOfElements" parameter greater than the difference between source index and dest. index.
Copies a specified number of elements from the specified one-dimensional source array, starting at the specified source index; the elements are copied to the specified one-dimensional destination array, starting at the specified destination index.

Source and destination can be the same array as well as two different arrays.

Usage
Array_Copy(SourceArray,SourceIndex,DestinationArray,DestinationIndex,NumberOfElements)

Where: SourceArray - an expression specifying the name of the source array
DestinationArray - an expression specifying the name of the destination array
SourceIndex - a numerical expression specifying the starting index for the source array
DestinationIndex - a numerical expression specifying the starting index for the destination array
NumberOfElements - a numerical expression specifying the number of elements to copy

Example
Copy from Array1 the two-element segment beginning at the index of 4, to the Array2, beginning at the index of 6:

Array_Copy(Array1,4,Array2,6,2);

Copy from Array1 the two-element segment beginning at the index of 4, to the same array, beginning at the index of 6:

Array_Copy(Array1,4,Array1,6,2);

Code: Select all

var:timewindow(false); timewindow = d=1240119 and time >= 800 and t <= 1000; array: int arrOne[10](0); once (true) begin array_setintegervalue(arrOne,1,1); array_setintegervalue(arrOne,2,2); array_setintegervalue(arrOne,3,3); array_setintegervalue(arrOne,4,4); array_setintegervalue(arrOne,5,5); array_setintegervalue(arrOne,6,6); end; //Code--> if timewindow and value10>0 //skip first round then begin array_copy(arrOne,1,arrOne,2,3); print ( t," COPY 1:", array_getintegervalue(arrOne,1), " 2:",array_getintegervalue(arrOne,2)," 3:",array_getintegervalue(arrOne,3) ," 4:",array_getintegervalue(arrOne,4)," 5:",array_getintegervalue(arrOne,5)," 6:",array_getintegervalue(arrOne,6) ); end; if timewindow then begin value10+=1; //Counter array_setintegervalue(arrOne,1,value10); print( t," new val: ", array_getintegervalue(arrOne,1) ); end; //<-- //Log if timewindow then begin print ( t," 1:", array_getintegervalue(arrOne,1), " 2:",array_getintegervalue(arrOne,2)," 3:",array_getintegervalue(arrOne,3), " 4:",array_getintegervalue(arrOne,4)," 5:",array_getintegervalue(arrOne,5)," 6:",array_getintegervalue(arrOne,6) ); end;

User avatar
Kate MultiCharts
Posts: 597
Joined: 21 Oct 2020
Has thanked: 9 times
Been thanked: 148 times

Re: array_copy

Postby Kate MultiCharts » 08 Feb 2024

Hello,

When an array is being copied into itself (array_copy(arrOne,1,arrOne,2,3);), the array overwrites its elements with its first element.

Code: Select all

array: int arrOne[10](0); once (true) begin array_setintegervalue(arrOne,1,1); array_setintegervalue(arrOne,2,2); array_setintegervalue(arrOne,3,3); array_setintegervalue(arrOne,4,4); array_setintegervalue(arrOne,5,5); array_setintegervalue(arrOne,6,6); end; if LastBarOnChart then begin once begin array_copy(arrOne,1,arrOne,2,3); for value1 = 1 to 6 begin print(value1, " ", arrOne[value1]); end; end; end;
Output

Code: Select all

1.00 1.00 2.00 1.00 3.00 1.00 4.00 1.00 5.00 5.00 6.00 6.00

BD.
Posts: 19
Joined: 02 Feb 2024
Has thanked: 2 times
Been thanked: 2 times

Re: array_copy

Postby BD. » 14 Feb 2024

You're right. It does actually work when array_copy(arrOne,1,arrOne,5,4); source Index doesn't overlap destination Index.

Code: Select all

1.00 1.00 2.00 2.00 3.00 3.00 4.00 4.00 5.00 1.00 6.00 2.00 7.00 3.00 8.00 4.00 9.00 9.00


Return to “MultiCharts”