在表达式中使用数组的已删除索引后,它会重新出现在 awk 中

在表达式中使用数组的已删除索引后,它会重新出现在 awk 中

我发现了奇怪的行为awk。我想删除一个数组元素,但发现,如果我在删除后在代码中的某个地方使用该元素(只是一个索引,没有值),它会再次出现。这是预期的行为吗?

awk '
# This function just for clarity and convenience.
function check(item) {
    if(item in arr) 
        printf "the array index \"%s\" exists\n\n", item 
    else 
        printf "the array index \"%s\" does not exist\n\n", item 
}

END {
    # Create element of array with index "f"
    arr["f"] = "yes"

    printf "The value of arr[\"f\"] before deleting = \"%s\"\n", arr["f"]

    # The first checking of the array - the index exists
    check("f")

    # Then delete this element
    # I am expecting no this element in the "arr" now
    delete arr["f"]

    # The second checking of the array - the index does not exist
    # as I were expecting
    check("f")

    # Use the non-existent index in expression
    printf "The value of arr[\"f\"] after deleting = \"%s\"\n", arr["f"]

    # The third checking of the array - the index exists again
    check("f")
}' input.txt

输出

The value of arr["f"] before deleting = "yes"
the array index "f" exists

the array index "f" does not exist

The value of arr["f"] after deleting = ""
the array index "f" exists

答案1

这是预期的行为。如果变量尚不存在,则引用变量的值将创建它。否则,以下内容将是语法错误:

$ awk 'BEGIN { print "Foo is " foo[0]; foo[0]="bar"; print "Foo is " foo[0]; delete foo[0]; print "Foo is " foo[0] }'
Foo is
Foo is bar
Foo is

即使对于非数组变量也是如此,但由于平面变量(有时)没有delete运算符,因此在问题中不涉及数组的情况下,这种情况不会经常出现。

答案2

您遇到的行为是因为此行以静默方式重新创建您之前删除的数组项:

printf "The value of arr[\"f\"] after deleting = \"%s\"\n", arr["f"]

看这个小测试:

$ awk 'BEGIN{a[1];delete a[1];for (i in a) print i}'
# nothing is printed
$ awk 'BEGIN{a[1];delete a[1];a[1];for (i in a) print i}'
1
$ awk 'BEGIN{a[1];delete a[1];print "a[1]=",a[1];for (i in a) print "key found:",i}'
a[1]= 
key found: 1

相关内容