Display contents of array at once

I want to display contents of a array at once. In detail, what I want to do is, I need to create a sting in which contents of array is stored.
This is required because I am using a task to display a characters and that task requires string.

  1. In case of printing characters directly on screen, what I want to do can be realized like:

logic extra_bit ;
int loop = 10;
extra_bit = new[loop];
for(int i=0;i < loop; i++)
extra_bit=$urandom%2;

$write(“Contents of array: “); // display
for(int 1=0; i< loop; i++) // display
$write(”%b ,”,extra_bit[i]) // display
$write(“\n”); // display

Above code will produce a message like:


Contents of array: 1, 0, 1, 1, 0, 0, 0, 0, 1, 0

Now I need to create a string storing the characters generated by the code:
“for(int 1=0; i< loop; i++)
$write(”%b ,",extra_bit[i])
"

Note: “,” is inserted as a delimiter.

How I can do this?

Regards,
Matsumoto

You can use $sformatf(“format”,arg1,arg2) which returns a string that you can assign to a string variable, or pass as a string argument to a task. You can replace your for loop with

foreach (extra_bit[i]) strvar = {strvar,$sformatf("%b ,", extra_bit[i]);

You can also do this in one statement using %p if you are OK with the simple formatting it provides.

strvar = $sformatf("%p", extra_bit);

In reply to dave_59:

Thank you very much.

I can do exactly what I wanted to do with the code.

( I learned “%p” is available and the way concatinating strings. )

Regards.