How do I print the entire associative array. I need to print all the keys and their corresponding values. Both key and value are integers. My array is of the form: int array [*]
In reply to AMARDEEP R PALURU:
The associative array is an unpacked array. So, individual elements need to be assigned and printed with the help of methods provided like first(), last(), next() etc. For example
array[50] = 20; // index 50 have value 20.
Never declare an associative array with a wildcard index[*]. You can not retrieve any key values because its type is unknown. Use [int] if you mean the index to mean a 32-bit integer. Then you can use a foreach loop to iterate over the array
int array[int]
foreach(array[key]) $display("key: %d value: %d", key, array[key]);
In reply to dave_59:
hi dave how to print if key is string type ?
In reply to pawan:
Use
%s instead of
%d.
In reply to dave_59:
no but we cant use foreach loop right ?
In reply to pawan:
Yes you can.
Hi Dave and pawan, how to print if key and array value are both strings.
string array[string]
foreach(array[key]) $display("key: %s value: %s", key, array[key]);
will this work?
In reply to prithvi123:
Try it?
In reply to dave_59:
we can also do this…
//declare and initialize an associative array
int array[string]='{"lala":20, "bt":3414};
//use %p to print all member of variable
$display("my_associative array %p", array);