Systemverilog Foreach Syntax for Looping Through Lower Dimension of Multidimensional Array

What is the standard way of looping through the lower dimension of a multidimensional array? With the higher dimension fixed.

In the following example:

  automatic int i = 2;
  foreach (my_req[i][j]) begin // <-- WARNING
    $display("i:%0d,j:%0d", i, j);
  end

I see the warning:

** Warning: testbench.sv(16): (vlog-LRM-2897) Using non-standard foreach loop variable list syntax.

Full code example on EDA Playground:

2 Answers

You can do this:

$display("Loop through i=2");
begin
  automatic int i = 2;
  foreach (my_req[,j]) begin // notice the "," before j
    $display("i:%0d,j:%0d", i, j);
  end
end

Working code on EDA Playground:

2

The warning is to let you know that the code you are writing may not be compatible with other simulators. I do know that at one time, another simulator accepted foreach (array[i][j]) where i was not previously defined to mean the same thing as foreach(array[i,j]), and that syntax would not be compatible with what you are trying to do.

The correct syntax is foreach (my_req[,j])

3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Robert Thorne

Robert Thorne

Automotive & Future Transportation Editor

Robert Thorne covers electric vehicle innovations, autonomous driving systems, global mobility trends, and automotive engineering developments.

Share this article
Twitter Facebook Pinterest