How to copy the elements of dynamic array in the another dynamic array considering some particular sequence

for eg: we have first dynamic array of 8 elements say:
dyn1=new[8];
dyn[0]=2;dyn[1]=1;dyn[2]=4;dyn[3]=6;dyn[4]=9;dyn[5]=7;dyn[6]=14;dyn[7]=65;
dyn2=new10;
dyn2[0]=4;dyn2[1]=0;
then the result i am getting is 2,1,4,6,9,7,14,65,0,0;
and if i am making it as dyn2[8]=4,dyn2[9]=0;
its displaying as 2,1,4,6,9,7,14,65,4,0;

but what if i want to get the print as 4,0,2,1,4,6,9,7,14,65? means first to display the newly written value on dyn2 and then the copied values of dyn1??

In reply to divya.mahajan:

You can try following code.


dyn2 = {4,0,dyn2};

incase if the data is stored in array then use:


dyn2 = {arr[0:1],dyn2};

Its better to use queue if you want to add/delete elements at random places.

Given that there are typos in your example here, I am assuming there may be other typos in your code. It always helps if you can cut and paste a complete example. The result I get after changing dyn to dyn1, and completing the rest of your code is

# '{4, 0, 4, 6, 9, 7, 14, 65, 0, 0}

If you will be frequently adding array elements, it is better to use a queue or an associative array than an dynamic array. As a Queue, your code can be written as

module top;
   int dyn1[$],dyn2[$];
   initial begin
      dyn1={2, 1, 4, 6, 9, 7, 14, 65};
      dyn2= {4,1,dyn1};
      // the most efficent way to add items to a queue is to push() or insert().
      // dyn2 = dyn1;
      // dyn2.push_front(0);
      // dyn2.push_front(4);
      $display("%p",dyn2);
   end
endmodule