The best and most concise way to find the value of the last element in an associative array?

In reply to rzhang313:

From which LRM section you got that the find_last() method is for associative arrays?
Array locator methods (find_last() is one of them) operate on any unpacked array, including queues, they return a queue, which makes the code you posted as second method not to make sense since you are casting a queue to an int.
What exactly you mean by the last element in an associative array? Also I think you could be misinterpreting the last() method as in the LRM is stated that it returns the last (largest)index or key
For example


module test();
  int aa[string]= '{"x":12, "b":23};
  string key;
  initial begin
    aa["a"]=90;
    if(aa.last(key))
      $display("the last of aa =%p is %p index = %p",aa, aa[key], key);
  end
endmodule

The last method returns “x” in one simulator as it is the largest key value used to store the array.

I think the return value of .last() is required for type checking and/or if the array is empty (returns zero), the LRM specifies the following:
The argument passed to first(), last()…"shall be assignment compatible with the index type of the array. If the argument has an integral type that is smaller than the size of the corresponding array index type, then the function returns –1 and shall truncate in order to fit into the argument.

I do not see such a huge overhead of having one if-else, that actually takes care the possibility of having an empty array, I’m not sure in your application what is the problem, even using something like what you propose how can you tell of the array is empty or if the type doesn’t match (which I think it should be an error detected at compile time, instead of handling this especial case of truncation for integral type indexes described in the LRM)

-R