Why is the value of "f" not consistent?

The output for the following code:


module tb;
initial begin
  int array [string];
  string f = "BB";
  array ='{"AA":1,"BB":4,"F":5,"G":7};
  
  $display("Assoc array is %p", array);
  
  $display("f is = %s and array.next(f) = %d", f, array.next(f));
  
  if(array.next(f))
  	$display("Array.next(%s) is = %d", f, array[f]);
end
endmodule

is

Assoc array is '{"AA":1, "BB":4, "F":5, "G":7} 
f is = F and array.next(f) = 1
Array.next(G) is = 7

Why does printing “f” give me “F” instead of “BB” (which is the value that I initialized the string with). And subsequently, how does F change to G in the next $display statement?

In reply to vk7715:

The answer is in section 7.9.6 Next() of the LRM:

The syntax for the next() method is as follows:

**function int next( ref index );**

where index is an index of the appropriate type for the array in question. Associative arrays that
specify a wildcard index type shall not be allowed.

The next() method finds the smallest index whose value is greater than the given index argument.

If there is a next entry, the index variable is assigned the index of the next entry, and the function
returns 1. Otherwise, the index is unchanged, and the function returns 0.

Calling next() modifies the index variable provided, so ‘f’ is modified by the function call. Also, the return value is a ‘0’ or ‘1’, so your second $display() call is likely not what you want.

From IEEE Std 1800-2017, section 7.9.6 Next(), the next function prototype is:


function int next( ref index );

The index variable is passed as a “ref” type, which means it can be modified by the function:

If there is a next entry, the index variable is assigned the index of the next entry

You probably get F instead of BB due to non-determinism. You should get BB if you separate the display into 2 display statements. F changes to G because next updates the f variable.


module tb;
initial begin
  int array [string];
  string f;
  f = "BB";
  array = '{"AA":1,"BB":4,"F":5,"G":7};
 
  $display("Assoc array is %p", array);
  $display("f=%s", f);
  $display("array.next(f)=%0d", array.next(f));
 
  if(array.next(f))
  	$display("array.next(%s)=%0d", f, array[f]);
end
endmodule

In reply to cgales:

Hi cgales, when you mean “smallest index whose value is…”, do you mean the value at the index or the index value** itself?

For example in an associative array as follows:


int array [string];
array = '{"apple":0, "banana":5, "grapes":6};

Index value of “banana” is 5 or the length of the string “banana”?

Please let me know.

In reply to vk7715:

cgales means the index values itself. The index value of “banana” is “banana”

In reply to dave_59:

Hi Dave,

Thank you for clarifying. When we compare index values where the index is a string, do we use alphabetical order or length of the string in determining which index value is greater or smaller than the other?

For example, in the associative array


array = '{"apple":0, "banana":5, "grapes":6};

Is index value “apple” greater than “grapes” or vice versa? If we use length of the string, “grapes” is greater, but if we go by alphabetical order, “apple” is greater.

Please let me know.

Thanks

In reply to vk7715:

See section 7.8.2 String index.