如何创建一个空数组

发布于 2024-12-10 12:29:41 字数 895 浏览 5 评论 0原文

更新

下面的原始描述有很多错误; 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

多情出卖 2024-12-17 12:29:41

我认为您的代码中可能存在一些拼写错误。

if (thread in threads_start) { // LINT warning here (you think)

在这里,您在数组 threads_start 中查找索引 thread

  printf("%s started at %d\n", threads[thread_start])); // Actual LINT warning

但在这里,您打印了数组 threads 中的索引 thread_start!另请注意不同 sthread/threadsthreads_start/thread_start。 Gawk 实际上是在第二行正确地警告您有关 thread_start(不带 s)的使用。

您的 printf 格式也有错误。

当您更改这些时,lint 警告就会消失:

if (thread in threads_start) {
  printf("%s started at %d\n", thread, threads_start[thread]));
} else {
  printf("%s started at unknown\n");
}

但也许我误解了您的代码应该做什么。在这种情况下,您可以发布一个最小的独立代码示例来生成虚假的 lint 警告吗?

I think you might have made a few typo's in your code.

if (thread in threads_start) { // LINT warning here (you think)

Here you look for the index thread in array threads_start.

  printf("%s started at %d\n", threads[thread_start])); // Actual LINT warning

But here you print the index thread_start in array threads! Also notice the different s's thread/threads and threads_start/thread_start. Gawk is actually warning you correctly about the usage of thread_start (without s) on the second line.

There also is an error in your printf format.

When you change these the lint warning disappears:

if (thread in threads_start) {
  printf("%s started at %d\n", thread, threads_start[thread]));
} else {
  printf("%s started at unknown\n");
}

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?

远山浅 2024-12-17 12:29:41

总结

创建空数组的惯用方法awk就是使用split()

详细信息

为了简化上面的示例以专注于您的问题而不是拼写错误,可以使用以下命令触发致命错误:

BEGIN{
    LINT = "fatal"; 
    if (thread in threads_start) { 
        print "if"; 
    } else {  
        print "not if"; 
    }
}

这会产生以下错误:

gawk: cmd。 line:3: fatal: 引用未初始化的变量 `thread'

在使用 thread 搜索 threads_start 之前给它一个值,传递 linting:

BEGIN{
    LINT = "fatal"; 
    thread = 0;
    if (thread in threads_start) { 
        print "if"; 
    } else {  
        print "not if"; 
    }
}

产生:

not if

要使用未初始化的数组创建 linting 错误,我们需要尝试访问不存在的条目:

BEGIN{
    LINT = "fatal"; 
    thread = 0;
    if (threads_start[thread]) { 
        print "if"; 
    } else {  
        print "not if"; 
    }
}

产生:

gawk: cmd。 line:4: fatal: 引用未初始化的元素 `threads_start["0"]'

所以,你实际上并不需要在 Awk 中创建一个空数组,但是如果你 >想要这样做,并回答你的问题,使用split()

BEGIN{
    LINT = "fatal"; 
    thread = 0;
    split("", threads_start);
    if (thread in threads_start) { 
        print "if"; 
    } else {  
        print "not if"; 
    }
}

产生:

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:

BEGIN{
    LINT = "fatal"; 
    if (thread in threads_start) { 
        print "if"; 
    } else {  
        print "not if"; 
    }
}

which produces the following error:

gawk: cmd. line:3: fatal: reference to uninitialized variable `thread'

Giving thread a value before using it to search in threads_start passes linting:

BEGIN{
    LINT = "fatal"; 
    thread = 0;
    if (thread in threads_start) { 
        print "if"; 
    } else {  
        print "not if"; 
    }
}

produces:

not if

To create a linting error with an uninitialised array, we need to attempt to access an non-existent entry:

BEGIN{
    LINT = "fatal"; 
    thread = 0;
    if (threads_start[thread]) { 
        print "if"; 
    } else {  
        print "not if"; 
    }
}

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():

BEGIN{
    LINT = "fatal"; 
    thread = 0;
    split("", threads_start);
    if (thread in threads_start) { 
        print "if"; 
    } else {  
        print "not if"; 
    }
}

produces:

not if

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文