[英]awk, custom delarray function
有人可以解釋為什么我沒有得到預期的結果嗎?
awk '
# I know there is delete array, but this is more portable
# that is what docs are saying, anyway wanted to test it out.
function delarray(a, i)
{
for (i in a)
delete a[i]
}
BEGIN {
a[3]=""
a[4]=""
for (e in a)
print e
delarray(a)
for (e in a)
print ".."
print e
}
'
執行上述腳本,我希望看到:
3
4
..(nothing here)
我曾經以為..
因為刪除了數組值,所以我看不到其他任何東西,所以只是將..
視為占位符)
,但我看到的實際輸出是:
3
4
4 #(why this?, and where are two dots?)
,同時退出代碼為1,為什么呢?
您在第二個循環中缺少花括號。
for (e in a) {
print ".."
print e
}
輸出:
3
4
您的刪除功能起作用了。
由於您缺少for (e in a)
循環中的花括號,因此它僅包含print ".."
語句,這就是為什么看不到任何點的原因。
print e
命令僅打印分配給e
的最后一個值(來自上一個for (e in a)
循環的值),即4
。
但是您的功能不是很有用,因為幾乎所有版本的awk
允許delete a
沒有索引delete a
命令。 它符合POSIX標准。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.