# Delete an element from queue by name

**URL:** <https://verificationacademy.com/forums/t/delete-an-element-from-queue-by-name/36749>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog, queues-systemverilog\
**Created:** [October 31, 2019, 7:24pm UTC](https://verificationacademy.com/forums/t/delete-an-element-from-queue-by-name/36749 "2019-10-31T19:24:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![amitgarg](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/a/c4cdca/32.png) [@amitgarg](https://verificationacademy.com/forums/u/amitgarg)\
**Post date:** [October 31, 2019, 7:24pm UTC](https://verificationacademy.com/forums/t/delete-an-element-from-queue-by-name/36749/1 "2019-10-31T19:24:40Z")

</div>

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’**

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [October 31, 2019, 8:00pm UTC](https://verificationacademy.com/forums/t/delete-an-element-from-queue-by-name/36749/2 "2019-10-31T20:00:00Z")

</div>

_In reply to [sv\_uvm\_learner\_1](https://verificationacademy.com/forums/t/delete-an-element-from-queue-by-name/36749):_

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

```verilog
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

```

---

<div class="post-metadata">

**Author:** ![amitgarg](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/a/c4cdca/32.png) [@amitgarg](https://verificationacademy.com/forums/u/amitgarg)\
**Post date:** [October 31, 2019, 8:54pm UTC](https://verificationacademy.com/forums/t/delete-an-element-from-queue-by-name/36749/3 "2019-10-31T20:54:35Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/delete-an-element-from-queue-by-name/36749/2):_

Thanks Dave.

---

<div class="post-metadata">

**Author:** ![KillSteal](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/k/74df32/32.png) [@KillSteal](https://verificationacademy.com/forums/u/KillSteal)\
**Post date:** [October 31, 2019, 9:24pm UTC](https://verificationacademy.com/forums/t/delete-an-element-from-queue-by-name/36749/4 "2019-10-31T21:24:40Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/delete-an-element-from-queue-by-name/36749/3):_

Hi Dave,

As always, thanks for the answer.

Not to nitpick, but I think you meant::

```verilog
  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.

---

<div class="post-metadata">

**Author:** ![bhimac](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/bhimac/32/3208_2.png) [@bhimac](https://verificationacademy.com/forums/u/bhimac)\
**Post date:** [November 10, 2019, 7:06pm UTC](https://verificationacademy.com/forums/t/delete-an-element-from-queue-by-name/36749/5 "2019-11-10T19:06:24Z")

</div>

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

```verilog

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

```
