Emman
Emman

Reputation: 1244

Method to delete particular index in dynamic array

Is there any other method to delete a particular index value from the dynamic array?

Here is my eg to delete index 2 of array a which I am storing it into array b

module top;
  bit[3:0] a [];
  bit[3:0] b [];
  int k=0;
  initial 
  begin
    a={5,6,7,8,9};
    foreach (a[i])
      $display ("a[%0d]:%d",i,a[i]);   
     b = new [a.size-1]; 
    foreach (a[i]) begin
      if (i == 2) begin
       continue; 
      end
        b[k++] = a[i]; 
    end
    foreach (b[i])
      $display ("b[%0d]:%d",i,b[i]);
  end
endmodule

output:

a[0]: 5
a[1]: 6
a[2]: 7
a[3]: 8
a[4]: 9
b[0]: 5
b[1]: 6
b[2]: 8
b[3]: 9

Upvotes: 0

Views: 7946

Answers (1)

dave_59
dave_59

Reputation: 42698

No. Dynamic arrays are designed to be allocated as a whole. Queues are what you want—they are specifically designed for addition and deletion of one element at a time.

module top;
  bit[3:0] a [];
  bit[3:0] b [$];
  initial 
  begin
    a={5,6,7,8,9};
    foreach (a[i])
      $display ("a[%0d]:%d",i,a[i]);   
     b = a
     b.delete(2);
    foreach (b[i])
      $display ("b[%0d]:%d",i,b[i]);
  end
endmodule

Upvotes: 2

Related Questions