Delete all the entries of queue

How to delete all the elements of queue using foreach loop?
Check whether this is correct:

////////////////////////
int q [$];
//queue loaded with large data//
foreach(q[i])
q.delete(i);
//////////////////////

will this snippet deletes all the contents of queue “q”?

In reply to kireeti1192:

Did you try it? What happens when you run this code? Why do you have to use a foreach loop?

In reply to cgales:

Yeah, I had tried.
When I run this code the elements in queue are not getting deleted.

I come to know that q.delete() will delete all the items in the queue (q). Is this correct?

In reply to kireeti1192:

No it’s better to empty your queue this way:

q= {};

Let say your queue is:

q = '{143bb6ae, f922dc1b, d026067f, 09cff0d9, a3d942f3, bca9699a, 1125b1bb, 73b7577c, b08ba496, 57ca744b}

It will stop at half the queue

i=0 - Queue Size=9 - Emptying test_q= '{f922dc1b, d026067f, 09cff0d9, a3d942f3, bca9699a, 1125b1bb, 73b7577c, b08ba496, 57ca744b}

i=1 - Queue Size=8 - Emptying test_q= '{f922dc1b, 09cff0d9, a3d942f3, bca9699a, 1125b1bb, 73b7577c, b08ba496, 57ca744b}

i=2 - Queue Size=7 - Emptying test_q= '{f922dc1b, 09cff0d9, bca9699a, 1125b1bb, 73b7577c, b08ba496, 57ca744b}

i=3 - Queue Size=6 - Emptying test_q= '{f922dc1b, 09cff0d9, bca9699a, 73b7577c, b08ba496, 57ca744b}

i=4 - Queue Size=5 - Emptying test_q= '{f922dc1b, 09cff0d9, bca9699a, 73b7577c, 57ca744b}

Check it with this code:


  int test_q[$];
  int val;

       // Fill in the queue
       for (i=0; i<10; i++)
          begin
            val = $urandom_range(32'hffff_ffff,0);
            if ( ( val inside {test_q} ) == 0 )
              begin
                test_q = {val , test_q};
                $displayh("test_q = %p", test_q);
              end
          end

        foreach (test_q[i])
          begin
            test_q.delete(i);
            $displayh("i=%0d - Queue Size=%0d - Emptying test_q= %p", i, test_q.size(), test_q);
          end


In reply to kireeti1192:

Again, did you try it? What happens when you run this code? Did you read the SystemVerilog LRM?