Looping over a 3d Systemverilog array

Status
Not open for further replies.

shaiko

Advanced Member level 5
Joined
Aug 20, 2011
Messages
2,644
Helped
303
Reputation
608
Reaction score
297
Trophy points
1,363
Visit site
Activity points
18,302
Hello,

I have a 3d array defined as :
Code:
logic [1:0] [2:0] [3:0] some_3d_array  ;
I want to write a "foreach" loop that iterates over all dimensions.
Is this the correct syntax ?
Code:
foreach (some_3d_array[i])
begin
    foreach (some_3d_array[i][j])
    begin
        foreach (some_3d_array[i][j][k])
        begin
        // do something
        end
    end
end
 

No. This syntax is not legal event if some tools allow it. You'll wind up iterating over the first dimension 3 times.

If you only want to do something at the inner most loop, you can do
Code:
foreach (some_3d_array[i,j,k])
      begin
      //do something 
     end

But if you really need 3 separate loops, you can do
Code:
foreach (some_3d_array[i])
begin
    // do something over i
    foreach (some_3d_array[,j])
    begin
        // do sometthing over j
        foreach (some_3d_array[,,k])
        begin
        // do something over k
        end
    end
end
 

    shaiko

    Points: 2
    Helpful Answer Positive Rating
Thanks dave_59.
What if my loop was defined as :
Code:
logic [1:0] [2:0] some_3d_array [3:0] ; // one unpacked dimension
How would the code change ?
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…