When size of a collection is unknown or the data space is sparse, an associative array is a better option. Associative arrays do not have any storage allocated until it is used, and the index expression is not restricted to integral expressions, but can be of any type.

An associative array implements a look-up table of the elements of its declared type. The data type to be used as an index serves as the lookup key and imposes an ordering.

Syntax

  
  
	// Value     Array_Name          [ key ];
	data_type    array_identifier    [ index_type ];

  

Initialization Example

  
  
module tb;
  	
	int   	array1 [int]; 			// An integer array with integer index
	int   	array2 [string]; 		// An integer array with string index
	string  array3 [string]; 		// A string array with string index
  
  	initial begin
      	// Initialize each dynamic array with some values
    	array1 = '{ 1 : 22,
	            	6 : 34 };
	            
		array2 = '{ "Ross" : 100,
	            	"Joey" : 60 };
	            
		array3 = '{ "Apples" : "Oranges",
	            	"Pears" : "44" };
      
      	// Print each array
      $display ("array1 = %p", array1);
      $display ("array2 = %p", array2);
      $display ("array3 = %p", array3);
    end
endmodule

  
Simulation Log

ncsim> run
array1 = '{1:22, 6:34}
array2 = '{"Joey":60, "Ross":100}
array3 = '{"Apples":"Oranges", "Pears":"44"}
ncsim: *W,RNQUIE: Simulation is complete.

Associative Array Methods

Function Description
function int num (); Returns the number of entries in the associative array
function int size (); Also returns the number of entries, if empty 0 is returned
function void delete ( [input index] ); index when specified deletes the entry at that index, else the whole array is deleted
function int exists (input index); Checks whether an element exists at specified index; returns 1 if it does, else 0
function int first (ref index); Assigns to the given index variable the value of the first index; returns 0 for empty array
function int last (ref index); Assigns to given index variable the value of the last index; returns 0 for empty array
function int next (ref index); Finds the smallest index whose value is greater than the given index
function int prev (ref index); Finds the largest index whose value is smaller than the given index

Associative Array Methods Example

  
  
module tb;
    int      fruits_l0 [string];
 
    initial begin
      fruits_l0 = '{ "apple"  : 4, 
                     "orange" : 10,
                     "plum"   : 9,
                     "guava"  : 1 };
       
      
      // size() : Print the number of items in the given dynamic array
      $display ("fruits_l0.size() = %0d", fruits_l0.size());
      
      
      // num() : Another function to print number of items in given array
      $display ("fruits_l0.num() = %0d", fruits_l0.num());
      
      
      // exists() : Check if a particular key exists in this dynamic array
      if (fruits_l0.exists ("orange"))
        $display ("Found %0d orange !", fruits_l0["orange"]);
      
      if (!fruits_l0.exists ("apricots"))
        $display ("Sorry, season for apricots is over ...");
      
      // Note: String indices are taken in alphabetical order
      // first() : Get the first element in the array
      begin
      	string f;
        // This function returns true if it succeeded and first key is stored
        // in the provided string "f"
        if (fruits_l0.first (f))
          $display ("fruits_l0.first [%s] = %0d", f, fruits_l0[f]);
      end
      
      // last() : Get the last element in the array
      begin
        string f;
        if (fruits_l0.last (f))
          $display ("fruits_l0.last [%s] = %0d", f, fruits_l0[f]);
      end
      
      // prev() : Get the previous element in the array
      begin
        string f = "orange";
        if (fruits_l0.prev (f))
          $display ("fruits_l0.prev [%s] = %0d", f, fruits_l0[f]);
      end
      
      // next() : Get the next item in the array
      begin
        string f = "orange";
        if (fruits_l0.next (f))
          $display ("fruits_l0.next [%s] = %0d", f, fruits_l0[f]);
      end
    end
endmodule

  
Simulation Log

ncsim> run
fruits_l0.size() = 4
fruits_l0.num() = 4
Found 10 orange !
Sorry, season for apricots is over ...
fruits_l0.first [apple] = 4
fruits_l0.last [plum] = 9
fruits_l0.prev [guava] = 1
fruits_l0.next [plum] = 9
ncsim: *W,RNQUIE: Simulation is complete.

Dynamic array of Associative arrays

  
  
module tb;
  // Create an associative array with key of type string and value of type int
  // for each index in a dynamic array
  int fruits [] [string];
  
  initial begin
    // Create a dynamic array with size 2
    fruits = new [2];
    
    // Initialize the associative array inside each dynamic array index
    fruits [0] = '{ "apple" : 1, "grape" : 2 };
    fruits [1] = '{ "melon" : 3, "cherry" : 4 };
    
    // Iterate through each index of dynamic array
    foreach (fruits[i])
      // Iterate through each key of the current index in dynamic array
      foreach (fruits[i][fruit])
        $display ("fruits[%0d][%s] = %0d", i, fruit, fruits[i][fruit]);
    
  end
endmodule

  
Simulation Log

ncsim> run
fruits[0][apple] = 1
fruits[0][grape] = 2
fruits[1][cherry] = 4
fruits[1][melon] = 3
ncsim: *W,RNQUIE: Simulation is complete.

Dynamic array within each index of an Associative array

  
  
// Create a new typedef that represents a dynamic array
typedef int int_da [];
 
module tb;
  // Create an associative array where key is a string 
  // and value is a dynamic array
  int_da fruits [string];
 
  initial begin
    // For key "apple", create a dynamic array that can hold 2 items 
    fruits ["apple"] = new [2];
    
    // Initialize the dynamic array with some values
    fruits ["apple"] = '{ 4, 5};
 
    // Iterate through each key, where key represented by str1
    foreach (fruits[str1]) 
      // Iterate through each item inside the current dynamic array ie.fruits[str1]
      foreach (fruits[str1][i])
        $display ("fruits[%s][%0d] = %0d", str1, i, fruits[str1][i]);
 
  end
endmodule

  
Simulation Log

ncsim> run
fruits[apple][0] = 4
fruits[apple][1] = 5
ncsim: *W,RNQUIE: Simulation is complete.