【发布时间】:2011-10-19 14:00:15
【问题描述】:
更新
下面的原文描述有很多错误; gawk lint 不会抱怨用作 in 的 RHS 的未初始化数组。例如,以下示例没有给出错误或警告。我没有删除这个问题,因为我即将接受的答案给出了使用 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]; }
【问题讨论】: