# Loop through nested struct in SV

**URL:** <https://verificationacademy.com/forums/t/loop-through-nested-struct-in-sv/48630>\
**Category:** SystemVerilog\
**Tags:** structure-and-union, loop\
**Created:** [October 1, 2024, 7:22am UTC](https://verificationacademy.com/forums/t/loop-through-nested-struct-in-sv/48630 "2024-10-01T07:22:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![TuanNg](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/t/85f322/32.png) [@TuanNg](https://verificationacademy.com/forums/u/TuanNg)\
**Post date:** [October 1, 2024, 7:22am UTC](https://verificationacademy.com/forums/t/loop-through-nested-struct-in-sv/48630/1 "2024-10-01T07:22:26Z")

</div>

Hi, I have a struct defined as a member of another struct, but somehow I cannot read the correct member of each struct as expected. The code is as:

```auto
typedef struct {
    string gender;  
    logic [4:0] start;    
    logic [4:0] ends;    
    int age;   
} studentInfoT;

typedef struct {
  string className;   
  logic [32-1:0] classLocation;   
  int classStudent;   
  studentInfoT memberInfo[string];
} classInfoT;

module test;

  classInfoT classes[3] = '{

                    '{"A1", 32'h0000_0004, 32, '{
                                                   "PETER" : '{"Male " , 0, 31, 12},
                                                   "ANNE" : '{"Female " , 0, 0, 13}
                                                   }}
                , '{"A2", 32'h0000_0002, 31, '{
                                                   "AMANDA" : '{"Female " , 0, 0, 14},
                                                   "DAVID" : '{"Male " , 31, 31, 12}
                                                   }}
                , '{"B1", 32'h0000_0001, 45, '{
                                                   "TOM" : '{"Male " , 0, 0, 13},
                                                   "BROWNY" : '{"Female " , 31, 31, 14}
                                                  }}                                                               
};

  initial begin
    foreach (classes[i]) begin
      $display("Class %0s -- %p", classes[i].className, classes[i]);
      $display("--------------------------------------------------");
      foreach (classes[i].memberInfo[s]) begin
        $display("Student name: %0s --> %p", s, classes[i].memberInfo[s]);
      end
      $display("----------------------------------------------------------------------------------------------------\n");
    end
  end

endmodule: test

```

---

<div class="post-metadata">

**Author:** ![TuanNg](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/t/85f322/32.png) [@TuanNg](https://verificationacademy.com/forums/u/TuanNg)\
**Post date:** [October 1, 2024, 7:25am UTC](https://verificationacademy.com/forums/t/loop-through-nested-struct-in-sv/48630/2 "2024-10-01T07:25:13Z")

</div>

Here I got this error, I expect to display ANNE and PETER for first class, AMANDA and DAVID for 2nd class only, and TOM and BROWNY for 3rd class but thing they didn’t display as expected, is it relared to the SV printing format or something?? Thank you for any support

> **Class A1 – ‘{className:“A1”, classLocation:‘h4, classStudent:32, memberInfo:’{“ANNE”:’{gender:"Female ", start:'h0, ends:‘h0, age:13}, “PETER”:’{gender:"Male ", start:'h0, ends:'h1f, age:12}} }**  
> **--------------------------------------------------**  
> **Student name: ANNE → '{gender:"Female ", start:'h0, ends:'h0, age:13}**  
> **Student name: PETER → '{gender:"Male ", start:'h0, ends:'h1f, age:12}**  
> **----------------------------------------------------------------------------------------------------**
> 
> **Class A2 – ‘{className:“A2”, classLocation:‘h2, classStudent:31, memberInfo:’{“AMANDA”:’{gender:"Female ", start:'h0, ends:‘h0, age:14}, “DAVID”:’{gender:"Male ", start:'h1f, ends:'h1f, age:12}} }**  
> **--------------------------------------------------**  
> **Student name: ANNE → '{gender:“”, start:'hxx, ends:'hxx, age:0}**  
> **Student name: DAVID → '{gender:"Male ", start:'h1f, ends:'h1f, age:12}**  
> **----------------------------------------------------------------------------------------------------**
> 
> **Class B1 – ‘{className:“B1”, classLocation:‘h1, classStudent:45, memberInfo:’{“BROWNY”:’{gender:"Female ", start:'h1f, ends:‘h1f, age:14}, “TOM”:’{gender:"Male ", start:'h0, ends:'h0, age:13}} }**  
> **--------------------------------------------------**  
> **Student name: AMANDA → '{gender:“”, start:'hxx, ends:'hxx, age:0}**  
> **Student name: BROWNY → '{gender:"Female ", start:'h1f, ends:'h1f, age:14}**  
> **Student name: TOM → '{gender:"Male ", start:'h0, ends:'h0, age:13}**  
> **----------------------------------------------------------------------------------------------------**

---

<div class="post-metadata">

**Author:** ![cgales](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/c/ccd318/32.png) [@cgales](https://verificationacademy.com/forums/u/cgales)\
**Post date:** [October 1, 2024, 1:42pm UTC](https://verificationacademy.com/forums/t/loop-through-nested-struct-in-sv/48630/3 "2024-10-01T13:42:26Z")

</div>

On EDA Playground, your code works as expected on 3 of the 4 simulators. One simulator seems to give incorrect results, which is likely a tool issue.
