How to assign and create associative array with multiple keys

Hi
i am trying to create an associative array with multiple keys. reason being i have a transaction which can be differentiated based on an id, then based on addr and then depending on number of bytes store the data. how can i achieve this and how can i assign values to this array.
for example i am thinking
typedef struct {bit [3:0] id; bit[11:0] addr; bit[31:0] byte_en;} key;
bit[255:0] data[key];

can i assign data[“4’b1,12’habc,32’hf”] = {256’hdeadbeef};
data[“4’b1,12’hdef,32’hf”] = {256’ha0a05a5a};
.
.
data[“4’b2,12’habc,32’hf”] = {256’hdeadbeef};
data[“4’b2,12’hdef,32’hf”] = {256’ha0a05a5a};
i tried this example on playground and i get compile error. is my declaration and assignment correct?
i am planning to retrieve the data using data_out = data[“4’b2,12’abc,32’hf”] where my data_out is 256bits.

In reply to shahparth08:

You have a number of syntax problems. You have an extra dynamic dimension in the declaration od data. You put the index in quotes"" when you should have used an assignment pattern. It would have helped to make your example a complete piece of code.

typedef struct {bit [3:0] id; bit[11:0] addr; bit[31:0] byte_en;} key;
bit[255:0] data[key];

module top;
  initial data[key'{4'b1,12'hdef,32'hf}] = {256'ha0a05a5a};
endmodule

This works on Questa, but for some of the older simulators on EDAplayground, you have to make the struct packed.

In reply to dave_59:

i was trying to make data as associative array since i want to find the elements in data array using one of the keys or even the entire combination of the keys.
like
data_out = data[key’{4’b1,12’habc,32’hf}].exists()?data[key’{4’b1,12’habc,32’hf}]:0;

i am trying to achieve something like this:

typedef struct {bit [3:0] id; bit[11:0] addr; bit[31:0] byte_en;} key;
bit[255:0] data[key];
bit[255:0] data_out; 
module top;
  initial 
    begin 
      data[key'{4'b1,12'hdef,32'hf}] = {256'ha0a05a5a};
      data[key'{4'b1,12'habc,32'hf}] = {256'hb0b05b5b};
      data_out = data[key'{4'b1,12'habc,32'hf}].exists()?data[key'{4'b1,12'habc,32'hf}]:0;
      $display ("data_out=%0d",data_out);
    end
endmodule

In reply to shahparth08:

data_out = data.exists(key’{4’b1,12’habc,32’hf})?data[key’{4’b1,12’habc,32’hf}]:0;

Please update your code with above line,then it works

In reply to rangasmile:

thanks that works. do you have any better suggestions for a situation like this any other usage model that is more efficient than above? regardless i think this is very effective solution but i was just wandering if there is something i am missing and can explore.

In reply to shahparth08:

When you declare an associative array, you can declare a default value for any uninitialized elements

bit[255:0] data[key] = '{default:0};

This does not allocate all keys into existence. But it allows a default value to be returned for a nonexistent element without a warning.

i found this thread where i want to implement something similar but to add on top of that i would also like to see how can i only search for id in the entire array and if it matches then matches then deelte, sort of LRU funcctionality .
Can anyone help on this ?

In reply to Blitzz0418:

You can do something like

module top;
  typedef struct {bit [3:0] id; bit[11:0] addr; bit[31:0] byte_en;} key;
  bit[255:0] data[key];
  key matching[$];
  initial 
    begin 
      data[key'{4'd1,12'hdef,32'hf}] = {256'ha0a05a5a};
      data[key'{4'd2,12'habc,32'hf}] = {256'hb0b05b5b};
      data[key'{id:4'd2, addr:12'habc, byte_en:32'h0}] = {256'hb0b05b5b};
      matching = data.find_index() with (item.index ==? key'{id:4'd2,addr:'z,byte_en:'z}) ;
      foreach(matching[i]) begin
        $displayh("deleting key %p", matching[i]);
        data.delete(matching[i]);
      end
      $displayh("data: %p", data);
    end
endmodule