Delete an element from queue by name

Hi,

i want to delete an element from queue. the delete method requires an index to delete the element.
i know the value which i want to delete. How can it be done ?

string queue_2[$];

initial begin
queue_2 = {“Red”,“Blue”,“Green”};

//i want to delete the BLUE element and i tried the below code but it end up with compilation error

//Delete Method

queue_2.delete(queue_2.find_first_index with (item == “BLUE”));
end

Below is error reported:
Arg. ‘index’ of ‘delete’: Cannot assign an unpacked type ‘int []’ to a packed type ‘int’

In reply to sv_uvm_learner_1:

All of the find_* methods return a queue type. This is so it can return an empty queue if there is no match. So you will need to do

string queue_2[$];
int blue_index[$];

initial begin
   queue_2 = {"Red","Blue","Green"};
   blue_index = queue_2.find_first_index with (item == "BLUE");
   foreach(blue_index[del]) queue_2.delete(del);
end

In reply to dave_59:

Thanks Dave.

In reply to dave_59:

Hi Dave,

As always, thanks for the answer.

Not to nitpick, but I think you meant::

  foreach(blue_index[del]) queue_2.delete(blue_index[del]);

Hi sv_uvm_learner_1, just note that, if you are deleting more than 1 of the same element, this method would get trickier and there are other posts on how to do it.

Hi here is the code for locating multiple elements and deleting them.


module mod;
string st_que[$];
int a_que[$];
int count;

initial begin
st_que={"Red","Green","Green","Blue","Green"};
a_que=st_que.find_index with (item=="Green");

foreach(a_que[i])
begin
st_que.delete(a_que[i]-count);
count++;
end

$display("Locations:%p",a_que);
$display("String Queue:%p",st_que);
end
endmodule