# Expression of this type cannot be used to index the array

**URL:** <https://verificationacademy.com/forums/t/expression-of-this-type-cannot-be-used-to-index-the-array/35426>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog, randomization-associative-array\
**Created:** [December 6, 2018, 9:17pm UTC](https://verificationacademy.com/forums/t/expression-of-this-type-cannot-be-used-to-index-the-array/35426 "2018-12-06T21:17:59Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![desperadorocks](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/d/ecc23a/32.png) [@desperadorocks](https://verificationacademy.com/forums/u/desperadorocks)\
**Post date:** [December 6, 2018, 9:17pm UTC](https://verificationacademy.com/forums/t/expression-of-this-type-cannot-be-used-to-index-the-array/35426/1 "2018-12-06T21:17:59Z")

</div>

Hello All !

Is it not possible to cycle through an associative array during randomization ? For the below code, get the following error.

```verilog

module assoc_array_cycling;
 class config;
  rand bit [7:0]my_pts[$];
 endclass: config

 class aac;
  config cfg;

  bit[7:0] agt_pts[string][$] = '{"alu0":'{0:'h11}, "cpu0":'{0:'h11, 1:'h22}};

  function perform_opr;
    cfg = new();

    assert(cfg.randomize with { 
           cfg.my_pts.size() == agt_pts["cpu0"].size();

           foreach(agt_pts["cpu0"][z])
             my_pts[z] == agt_pts["cpu0"][z];

    }) else $display("Randomization failed");
  endfunction: perform_opr
 endclass: aac 

 initial begin
   aac aac_inst = new();
 
   aac_inst.perform_opr();
 end
endmodule: assoc_array_cycling

```

Error Message:  
Error-[TCF-IIE] Illegal index expression  
assoc\_array\_cycling.sv, 19  
“this.my\_pts[z]”  
Expression of this type cannot be used to index the array

Can anyone clarify the same !

---

<div class="post-metadata">

**Author:** ![javatea](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/j/3d9bf3/32.png) [@javatea](https://verificationacademy.com/forums/u/javatea)\
**Post date:** [December 6, 2018, 11:19pm UTC](https://verificationacademy.com/forums/t/expression-of-this-type-cannot-be-used-to-index-the-array/35426/2 "2018-12-06T23:19:00Z")

</div>

_In reply to [desperadorocks](https://verificationacademy.com/forums/t/expression-of-this-type-cannot-be-used-to-index-the-array/35426):_

my\_pts = '{'h11, 'h22} // code display

```verilog

module assoc_array_cycling;
 class config;
  rand bit [7:0]my_pts[]; // using dynamic array instead of queue
 endclass: config
 
 class aac;
  config cfg;
 
  bit[7:0] agt_pts[string][$] = '{"alu0":'{'h11}, "cpu0":'{'h11,'h22}}; // your assignment is wrong
 
  function perform_opr;
    cfg = new();
    
    assert(cfg.randomize with { 
           cfg.my_pts.size() == agt_pts["cpu0"].size();
 
      foreach(my_pts[z]) // walking dynamic array
             my_pts[z] == agt_pts["cpu0"][z];
 
    }) else $display("Randomization failed");

    $display("my_pts = %p", cfg.my_pts);
  endfunction: perform_opr
 endclass: aac 
 
 initial begin
   aac aac_inst = new();
 
   aac_inst.perform_opr();
 end
endmodule: assoc_array_cycling

```

---

<div class="post-metadata">

**Author:** ![desperadorocks](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/d/ecc23a/32.png) [@desperadorocks](https://verificationacademy.com/forums/u/desperadorocks)\
**Post date:** [December 7, 2018, 12:11am UTC](https://verificationacademy.com/forums/t/expression-of-this-type-cannot-be-used-to-index-the-array/35426/3 "2018-12-07T00:11:46Z")

</div>

_In reply to [javatea](https://verificationacademy.com/forums/t/expression-of-this-type-cannot-be-used-to-index-the-array/35426/2):_

Hello Javatea,

Thanks for your response. This is a basic example to represent my bigger issue. In that scenario, I can’t edit the my\_pts code from queue to an dynamic array bcoz its a vip from a different team.

With that said, and even after changing the assignment code in the agt\_pts I face the same issue. Aren’t there any other way to solve this out ? or is it not possible at all ?

---

<div class="post-metadata">

**Author:** ![prabhakaran.gunasekaran](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/p/6bbea6/32.png) [@prabhakaran.gunasekaran](https://verificationacademy.com/forums/u/prabhakaran.gunasekaran)\
**Post date:** [December 7, 2018, 6:36am UTC](https://verificationacademy.com/forums/t/expression-of-this-type-cannot-be-used-to-index-the-array/35426/4 "2018-12-07T06:36:25Z")

</div>

_In reply to [desperadorocks](https://verificationacademy.com/forums/t/expression-of-this-type-cannot-be-used-to-index-the-array/35426/3):_

The below code seems working.

```verilog

class config_;
  rand bit [7:0]my_pts[$];
endclass: config_
 
class aac;
  config_ cfg; 
  bit[7:0] agt_pts[string][$] = '{"alu0":'{0:'h11}, "cpu0":'{0:'h11, 1:'h22}};
 
  function void perform_opr;
    cfg = new();
    assert(cfg.randomize with {
      cfg.my_pts.size() == agt_pts["cpu0"].size(); 
      //foreach(agt_pts["cpu0"][z])
      foreach(cfg.my_pts[z]) cfg.my_pts[z] == agt_pts["cpu0"][z];
    }) else $display("Randomization failed");
    
    foreach(cfg.my_pts[ii]) $display("my_pts[%0d] : %0h",ii,cfg.my_pts[ii]);
  endfunction: perform_opr
 endclass: aac
    
module assoc_array_cycling;
 aac aac_inst;
 initial begin
   aac_inst = new(); 
   aac_inst.perform_opr();
 end
endmodule: assoc_array_cycling

```

# Output:

ncsim\> run  
my\_pts[0] : 11  
my\_pts[1] : 22

---

<div class="post-metadata">

**Author:** ![desperadorocks](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/d/ecc23a/32.png) [@desperadorocks](https://verificationacademy.com/forums/u/desperadorocks)\
**Post date:** [December 7, 2018, 4:26pm UTC](https://verificationacademy.com/forums/t/expression-of-this-type-cannot-be-used-to-index-the-array/35426/5 "2018-12-07T16:26:03Z")

</div>

_In reply to [karan\_18](https://verificationacademy.com/forums/t/expression-of-this-type-cannot-be-used-to-index-the-array/35426/4):_

Hello Karan !

THanks for your inputs. Had the reverse working at my end as well. But was wondering if cycleling through the assoc array during randomization isn’t possible at all?

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [December 7, 2018, 5:17pm UTC](https://verificationacademy.com/forums/t/expression-of-this-type-cannot-be-used-to-index-the-array/35426/6 "2018-12-07T17:17:48Z")

</div>

_In reply to [desperadorocks](https://verificationacademy.com/forums/t/expression-of-this-type-cannot-be-used-to-index-the-array/35426/5):_

The syntax is not legal, though simulators other than the one you are using support it. You are only allowed one set of 's in a foreach loop. Normally you would do there following to skip of iterating one of the dimensions:

```verilog
foreach(agt_pts[,z])

```

But that doesn’t work when the z dimension is dynamically sized.

A few other non-standard code fragments

- **config** is a reserved keyword
- You cannot use an assignment pattern with named indexes to assign to a queue. Use array concatenation

```verilog
bit[7:0] agt_pts[string][$] = '{"alu0":{h11}, "cpu0":{h11,'h22}};

```

- You cannot initialize an implicitly static variable(aac\_inst) in its declaration. People mistakenly think it gets initialized as part of entering the procedural block it in.

---

<div class="post-metadata">

**Author:** ![desperadorocks](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/d/ecc23a/32.png) [@desperadorocks](https://verificationacademy.com/forums/u/desperadorocks)\
**Post date:** [December 7, 2018, 6:43pm UTC](https://verificationacademy.com/forums/t/expression-of-this-type-cannot-be-used-to-index-the-array/35426/7 "2018-12-07T18:43:12Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/expression-of-this-type-cannot-be-used-to-index-the-array/35426/6):_

THanks Dave for your inputs ! And thanks to all !
