如何创建一个空数组
更新
下面的原始描述有很多错误; 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]; }
UPDATE
The original description below has many errors; gawk lint does not complain about uninitialized arrays used as RHS of in
. For example, the following example gives no errors or warnings. I am not deleting the question because the answer I am about to accept gives good suggestion of using split
with an empty string to create an empty array.
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";
}
}
Original Question
A lot of my awk scripts have a construct as follows:
if (thread in threads_start) { // LINT warning here
printf("%s started at %d\n", threads[thread_start]));
} else {
printf("%s started at unknown\n");
}
With gawk --lint
which results in
warning: reference to uninitialized variable `thread_start'
So I initialize in the BEGIN block as follows. But this looks kludge-y. Is there a more elegant way to create a zero-element array?
BEGIN { LINT = 1; thread_start[0] = 0; delete thread_start[0]; }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您的代码中可能存在一些拼写错误。
在这里,您在数组
threads_start
中查找索引thread
。但在这里,您打印了数组
threads
中的索引thread_start
!另请注意不同 s 的thread
/threads
和threads_start
/thread_start
。 Gawk 实际上是在第二行正确地警告您有关thread_start
(不带 s)的使用。您的
printf
格式也有错误。当您更改这些时,lint 警告就会消失:
但也许我误解了您的代码应该做什么。在这种情况下,您可以发布一个最小的独立代码示例来生成虚假的 lint 警告吗?
I think you might have made a few typo's in your code.
Here you look for the index
thread
in arraythreads_start
.But here you print the index
thread_start
in arraythreads
! Also notice the different s'sthread
/threads
andthreads_start
/thread_start
. Gawk is actually warning you correctly about the usage ofthread_start
(without s) on the second line.There also is an error in your
printf
format.When you change these the lint warning disappears:
But perhaps I've misunderstood what your code is supposed to do. In that case, could you post a minimal self-contained code sample that produces the spurious lint warning?
总结
创建空数组的惯用方法awk就是使用
split()
。详细信息
为了简化上面的示例以专注于您的问题而不是拼写错误,可以使用以下命令触发致命错误:
这会产生以下错误:
gawk: cmd。 line:3: fatal: 引用未初始化的变量 `thread'
在使用
thread
搜索threads_start
之前给它一个值,传递 linting:产生:
not if
要使用未初始化的数组创建 linting 错误,我们需要尝试访问不存在的条目:
产生:
gawk: cmd。 line:4: fatal: 引用未初始化的元素 `threads_start["0"]'
所以,你实际上并不需要在 Awk 中创建一个空数组,但是如果你 >想要这样做,并回答你的问题,使用
split()
:产生:
not if
Summary
The idiomatic method of creating an empty array in Awk is to use
split()
.Details
To simplify your example above to focus on your question rather than your typos, the fatal error can be triggered with:
which produces the following error:
gawk: cmd. line:3: fatal: reference to uninitialized variable `thread'
Giving
thread
a value before using it to search inthreads_start
passes linting:produces:
not if
To create a linting error with an uninitialised array, we need to attempt to access an non-existent entry:
produces:
gawk: cmd. line:4: fatal: reference to uninitialized element `threads_start["0"]'
So, you don't really need to create an empty array in Awk, but if you want to do so, and answer your question, use
split()
:produces:
not if