更新
下面的原始描述有很多错误;gawk lint 不会抱怨用作 RHS 的未初始化数组in
。例如,以下示例没有给出错误或警告。我没有删除这个问题,因为我即将接受的答案给出了使用split
空字符串创建空数组的好建议。
BEGIN{
LINT = "fatal";
// print x; // LINT gives error if this is uncommented
thread = 0;
if (thread in threads_start) {
print "if";
} else {
print "not if";
}
}
原始问题
我的很多 awk 脚本都有如下结构:
if (thread in threads_start) { // LINT warning here
printf("%s started at %d\n", threads[thread_start]));
} else {
printf("%s started at unknown\n");
}
结果gawk --lint
是
警告:对未初始化变量“thread_start”的引用
所以我在 BEGIN 块中初始化如下。但这看起来很杂乱。有没有更优雅的方法来创建一个零元素数组?
BEGIN { LINT = 1; thread_start[0] = 0; delete thread_start[0]; }