Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Register Log in

Looping over a 3d Systemverilog array

shaiko

Advanced Member level 5
Joined
Aug 20, 2011
Messages
2,639
Helped
302
Reputation
606
Reaction score
297
Trophy points
1,363
Activity points
18,216
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
 

dave_59

Advanced Member level 3
Joined
Dec 15, 2011
Messages
812
Helped
361
Reputation
726
Reaction score
354
Trophy points
1,353
Location
Fremont, CA, USA
Activity points
6,271
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

shaiko

Advanced Member level 5
Joined
Aug 20, 2011
Messages
2,639
Helped
302
Reputation
606
Reaction score
297
Trophy points
1,363
Activity points
18,216
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 ?
 

Toggle Sidebar

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top