如何删除用户定义函数中的数组?

How to delete an array inside a user-defined function?

在 awk 中,可以使用以下命令删除先前定义的数组:

delete array;

或:

for (i in array)
{
    delete array[i];
}

是否可以定义一个将数组作为输入然后删除它的用户函数?例如:

function delarr(arrname)
{
    delete arrname;
}

BEGIN {
    array[1] = "1";
    array[2] = "2";

    print array[1];

    delarr(array);
}

你的例子很管用。

来自gawk manual

In awk, when you declare a function, there is no way to declare explicitly whether the arguments are passed by value or by reference.

Instead, the passing convention is determined at runtime when the function is called, according to the following rule: if the argument is an array variable, then it is passed by reference. Otherwise, the argument is passed by value.


POSIX 推荐相同的行为:

Function parameters shall be passed by value if scalar and by reference if array name.