Is it possible to declare an array with configuration?

Or any similar dynamic-length data structure that can easily be transferred to an array. The only workaround I found was to enter the array as a string and manually parse it.

config var not_array: string = '[1,2,3,4,5]' ; proc main() { // config array workaround writeln("I am string. Definitely not array ", not_array) ; // parse string not_array = not_array.replace(' ','') ; not_array = not_array.replace('[','') ; not_array = not_array.replace(']','') ; var still_not_array = not_array.split(',') ; // prepare array var dom = 0..#still_not_array.size ; var array: [dom] real ; // populate array for (i, x) in zip(dom, still_not_array) { array[i] = x:real ; } writeln("Ha! Tricked you, am actually array ", array) ; } 

It works as intended, but is there a better way?

+5
source share
1 answer

Is it possible to declare an array with configuration?

No, this is not yet supported in the Chapel, as in Chapel 1.16.

However, there are ways around this while you demonstrate.

As an alternative solution, you can use IO to write an input string to memory and then read it as an array, e.g.

 config type arrType = int; config const arrSize = 3, arrString = '1 2 3'; var A : [1..arrSize] arrType; // Create a memory buffer to write in var f = openmem(); // Create a writer on the memory buffer and write the input string var w = f.writer(); w.writeln(arrString); w.close(); // Create a reader on the memory buffer and read the input string as an array var r = f.reader(); r.readln(A); r.close(); writeln(A); 

Note that this requires an array size in front. I think you will need to do some line processing, as your original example, in order to calculate this on the fly.

Some resources:

+4
source

Source: https://habr.com/ru/post/1273935/


All Articles