5

Or any similar data structure of dynamic length, which, can be cast easily to an array. The only workaround I have found is entering the array as a string and manually parsing 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) ;
}

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

4

1 回答 1

4

是否可以使用配置声明数组?

不,这在 Chapel 1.16 中尚不支持。

也就是说,正如您演示的那样,有一些方法可以解决这个问题。

作为替代解决方法,您可以利用IO调用将输入字符串写入内存,然后将其作为数组读入,例如

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);

请注意,这需要预先设置数组大小。我认为您必须像原始示例一样进行一些字符串处理才能即时计算。

一些资源:

于 2017-12-08T15:44:25.077 回答