![在表达式中使用数组的已删除索引后,它会重新出现在 awk 中](https://linux22.com/image/129225/%E5%9C%A8%E8%A1%A8%E8%BE%BE%E5%BC%8F%E4%B8%AD%E4%BD%BF%E7%94%A8%E6%95%B0%E7%BB%84%E7%9A%84%E5%B7%B2%E5%88%A0%E9%99%A4%E7%B4%A2%E5%BC%95%E5%90%8E%EF%BC%8C%E5%AE%83%E4%BC%9A%E9%87%8D%E6%96%B0%E5%87%BA%E7%8E%B0%E5%9C%A8%20awk%20%E4%B8%AD.png)
我发现了奇怪的行为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