Why is the value of "f" not consistent?

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