0

アップデート

以下の元の説明には多くのエラーがあります。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]; }
4

2 に答える 2

1

コードにいくつかタイプミスをした可能性があると思います。

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

threadここでは、配列内のインデックスを探しますthreads_start

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

しかし、ここthread_startでは配列にインデックスを出力しますthreadsまた、 sthread/threadsthreads_start/が異なることに注意してthread_startください。thread_startGawkは、実際には2行目の(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");
}

しかし、おそらく私はあなたのコードが何をすべきかを誤解しました。その場合、偽のリント警告を生成する最小限の自己完結型コードサンプルを投稿できますか?

于 2011-11-08T11:39:48.960 に答える
0

概要

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'

パスリンティングのthread検索に使用する前に値を指定します。threads_start

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

生成:

not if

初期化されていない配列でリンティングエラーを作成するには、存在しないエントリへのアクセスを試みる必要があります。

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

于 2013-04-03T06:42:36.750 に答える