A basic introduction to associate arrays, showing initialization, .size(), and adding and deleting elements.
While fixed and dynamic arrays are useful for working with contiguous data, associative arrays allow inexpensive modelling of sparse data, by not requiring all indices to be modeled.
An associative array is a hash or lookup table, where the index (sometimes referred to as a key) sets the ordering for the data and provides a means to access it.
Method size() (which is same as num()), can be used to show the size of the array.
associative array example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| module aa;
integer aa [bit [15:0]];
initial begin
aa = '{2:5, 6:55, 7:3, 16'hFFFE:-6};
show();
aa.delete(6);
show();
aa[8] =33;
show();
end
function void show();
foreach (aa[i]) begin
$write("aa[%4h]=%0d, ",i,aa[i]);
end
$display("");
$display(" aa size =%0d",aa.size());
endfunction
endmodule // aa
|
.
//run command