【问题标题】:how to create an empty array如何创建一个空数组
【发布时间】: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]; }

【问题讨论】:

    标签: lint gawk


    【解决方案1】:

    我认为您的代码中可能出现了一些拼写错误。

    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!还要注意不同的 s 的 thread/threads 和 threads_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 警告的最小独立代码示例?

    【讨论】:

      【解决方案2】:

      总结

      creating an empty array in Awk的惯用方法是使用split()。

      详情

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

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

      这会产生以下错误:

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

      在使用 threads_start 搜索之前给 thread 一个值通过 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: reference to uninitialized element `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

      【讨论】:

      • 感谢您让我回到这个愚蠢的问题。感谢您忽略错误并仍然提供有用的答案。
      猜你喜欢
      • 2011-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-16
      • 2017-05-04
      • 1970-01-01
      • 2010-10-08
      相关资源
      最近更新 更多