Compare fields from two associative arrays

Hi,

I want to compare fields from two associative arrays. please guide me.

//sample code look a like

//Structs are shown here for reference
	typedef struct packed {
		logic [2:0] p;
		logic [3:0] q;
	}walk_order_t;
	
	typedef struct packed {
		logic [2:0] P;
		logic [3:0] Q;
	}walk1_order_t;
	
	typedef struct packed {
		logic [1:0] a;
		logic [1:0] b
		walk_order_t wk_order;  
	} key_1;
	
	typedef struct packed {
		logic [1:0] A;
		logic [2:0] B
		walk1_order_t wk_order_1;  
	} key_2;
	
	typedef struct packed {
		logic [2:0] c;
		logic [1:0] d;
	} val_1;

	typedef struct packed {
		logic [1:0] C;
		logic [3:0] D;
	} val_2;
	
	
	
	
	module adn;
		key_1 array1 [val_1];
		key_2 array2 [val_2];
		
		//based on some conditions values are loaded into these two associative arrays and they look a like below
		
**//First_array:	**	
		//Key1: '{a:2'd3, b:2'd2, p:3'd1, q:4'd2}  Value1: ' {c:3'd7, d:2'd0}
		//Key1: '{a:2'd0, b:2'd2, p:3'd1, q:4'd2}  Value1: ' {c:3'd7, d:2'd1}
		//Key1: '{a:2'd3, b:2'd2, p:3'd1, q:4'd2}  Value1: ' {c:3'd7, d:2'd2}
		//Key1: '{a:2'd3, b:2'd5, p:3'd4, q:4'd3}  Value1: ' {c:3'd7, d:2'd3}
		
		
		**//Second_array:**
		//Key2: '{A:2'd1, B:3'd2, P:3'd1, Q:4'd2}  Value2: ' {C:2'd7, D:4'd0}
		//Key2: '{A:2'd2, B:3'd2, P:3'd1, Q:4'd2}  Value2: ' {C:2'd7, D:4'd1}
		//Key2: '{A:2'd3, B:3'd5, P:3'd4, Q:4'd3}  Value2: ' {C:2'd7, D:4'd3}
		//Key2: '{A:2'd3, B:3'd2, P:3'd1, Q:4'd2}  Value2: ' {C:2'd7, D:4'd2}
		
		//Now I want to check if *things* are matching between two arrays. if so, print those key.
		
		if (array1.key1.a == array2.key.A) && (array1.key.b == arrar2.key.B[1:0]) && (array1.key.wk_order.p[1:0] == array2.key.wk_order_1.P[1:0]) 
			print  array1.key and print array2.key;
		
		if (array1.val_1.c[1:0] == array2.val_2.C[1:0])
			print array1.key and print array2.key;
		
	
	endmodule

In reply to nimbalkarad1:

Normally you would declare the associative array with the key as the index

val_1 array1 [key_1];
val_2 array2 [key_2];

Then you could do

if (array1['{a:2'd0, b:2'd2, wk_order:   '{p:3'd1, q:4'd2}}].c == 
    array2['{A:2'd1, B:3'd2, wk_order_1: '{P:3'd1, Q:4'd2}}].C)..

Note that since key_1/key2 are nested nested structures. you need to use a nested assignment pattern to label all the fields. Also, since these are all packed structures. you can use a concatenation. But then you lose the ability to name the fields and could have problems if you specify the incorrect sizes.

if (array1[{2'd0, 2'd2, 3'd1,4'd2}].c == 
    array2[{ 2'd1, 3'd2, 3'd1, 4'd2}].C)..

I suggest you also look at section 7.12.1 Array locator methods in the IEEE 1800-2017 SystemVerilog LRM which gives you a way to lookup elements by value and return the matching keys.

In reply to dave_59:

Hi Dave, could you please explain the syntax val_1 array1 [key_1]? I see that we are creating an “instance” of the struct val_1 and calling the instance ‘array 1’. But I do not understand how we can pass ‘key_1’ which is a struct, as the number of elements for the array.

In reply to vk7715:

See section 7.8 Associative arrays in the IEEE 1800-2017 SystemVerilog LRM.