Ready-to-run snippets

Code Examples

Ready-to-run Verilog, SystemVerilog, and UVM examples. Click any to open it live in the IDE.

← All examples

Interview Questions Set 4: How to find indices associated with associative array

SystemVerilog Interview Questions

Runnable example from the SystemVerilog Interview Questions Set 4 tutorial (How to find indices associated with associative array items?)

Open in IDE
testbench.sv
module tb;
  int   fruit_hash [string];
  string 	idx_q [$];
  
  initial begin
    fruit_hash["apple"] = 5;
    fruit_hash["pear"]  = 3;
    fruit_hash["mango"] = 9;
    
    idx_q = fruit_hash.find_index with (1);
    $display("idx_q= %p", idx_q);
  end
endmodule

// Output
// idx_q= '{"apple", "mango", "pear"} 
Admin mode — edit and delete controls are visible on each row. Use the IDE to add new snippets via File → Save as Snippet.