I’m having some problems with for-loop statement in system verilog.
module tb_top();
int num_q[$] = '{0,1,2,3,4,5,6,7,8,9,10};
int del_q[$] = '{1,2};
initial
begin
$display("num_q_size=%0d",num_q.size());
for(int ii = num_q.size(); ii >= 'd0; ii--)begin
$display("Loop count=%0d",ii);
if(num_q[ii-1] inside del_q)
num_q.delete(ii-1);
else
continue;
end
$display("value of num_q=%p",num_q);
end
endmodule
The above code runs repeatedly without no hope of stopping.
I don’t understand the reason. Can some one please explain
In reply to deepak_sadasivam_uvm:
add extra indx ques , loop through it to delete the index.
module tb_top();
int num_q[$] = '{0,1,2,3,4,5,6,7,8,9,10};
int del_q[$] = '{1,2};
int idx[$]; // index queue
initial
begin
$display("num_q_size=%0d",num_q.size());
foreach(del_q[i]) begin
idx = num_q.find_index with (item == del_q[i]);
if(idx.size != 0 ) begin
foreach(idx[i]) begin // loop through index value.
num_q.delete(idx[i]); // delete the index
end
end
end
$display("value of num_q=%p",num_q);
end
endmodule
In reply to kddholak:
Hi,
Thanks for answering but my question is more understand the below code:
Working code:
for(int ii = num_q.size(); ii > 'd0; ii--)begin
$display("Loop count=%0d",ii);
if(num_q[ii-1] inside del_q)
num_q.delete(ii-1);
else
continue;
end
Code which goes to infinite loop:
for(int ii = num_q.size(); ii >= 'd0; ii--)begin
$display("Loop count=%0d",ii);
if(num_q[ii-1] inside del_q)
num_q.delete(ii-1);
else
continue;
end
can you help here what going wrong.
In reply to deepak_sadasivam_uvm:
for(int ii = num_q.size(); ii >= 0; ii--)begin - remove 'd it will work
Issue is 'd0 - It is treating as unsigned integer 32'd0
ii will become 32'hffff_ffff after 0 and it is greater than 0
In reply to kddholak:
In reply to deepak_sadasivam_uvm:
add extra indx ques , loop through it to delete the index.
module tb_top();
int num_q[$] = '{0,1,2,3,4,5,6,7,8,9,10};
int del_q[$] = '{1,2};
int idx[$]; // index queue
initial
begin
$display("num_q_size=%0d",num_q.size());
foreach(del_q[i]) begin
idx = num_q.find_index with (item == del_q[i]);
if(idx.size != 0 ) begin
foreach(idx[i]) begin // loop through index value.
num_q.delete(idx[i]); // delete the index
end
end
end
$display("value of num_q=%p",num_q);
end
endmodule
Yes you provided the solution thanks for that, but what’s the issue? I think addressing the issue is more benficial than providing solution.