1

This code creates one record variable (r) and one tuple variable (t) that contain several arrays and prints them to stdout:

const N = 5;

record Myrec {
    var a: [1..N] int = (for i in 1..N do i);
    var b: [1..N] int = (for i in 1..N do i);
    var c: [1..N] int = (for i in 1..N do i);
}

proc test() {
    var r: Myrec;
    var t = (r.a, r.b, r.c);
    writeln( "r = ", r );
    writeln( "t = ", t );
}

test();

If I run this code, I get this output:

r = (a = 1 2 3 4 5, b = 1 2 3 4 5, c = 1 2 3 4 5)
t = (1 2 3 4 5, 1 2 3 4 5, 1 2 3 4 5)

but I feel the output is not very readable (particularly in the case of t). So, I am wondering if there is some way to print such variables with square brackets, e.g., like the following?

t = ([1 2 3 4 5], [1 2 3 4 5], [1 2 3 4 5])

I think it can be achieved by using writef() + a format string + passing each field of the tuple (or write a specific function for that purpose), but it would be nice if there is some convenient way to achieve a similar goal...

minibean
  • 263
  • 1
  • 7

1 Answers1

1

According to the docs of FormattedIO, it seems like the format string %ht (or %jt) does the job of adding [...] for array elements. A modified example code is attached below:

record Myrec {
    var n = 100;
    var a = [1,2,3];
    var x = 1.23;
}

proc test() {
    var r: Myrec;
    var t = (r.a, r.a);

    writeln();
    writeln( "r = ", r );
    writef( "r = %ht\n", r );  // "h" -> Chapel style
    writef( "r = %jt\n", r );  // "j" -> JSON style

    writeln();
    writeln( "t = ", t );
    writef( "t = %ht\n", t );

    var arr2d: [1..2, 1..3] int = (for i in 1..6 do i);

    writeln();
    writeln( "arr2d =\n", arr2d );
    writef( "arr2d =\n%ht\n", arr2d );
}

test();

Output

r = (n = 100, a = 1 2 3, x = 1.23)
r = new Myrec(n = 100, a = [1, 2, 3], x = 1.230000e+00)
r = {"n":100, "a":[1, 2, 3], "x":1.230000e+00}

t = (1 2 3, 1 2 3)
t = ([1, 2, 3], [1, 2, 3])

arr2d =
1 2 3
4 5 6
arr2d =
[
 [1, 2, 3],
 [4, 5, 6]
]
minibean
  • 263
  • 1
  • 7