How to define dynamic array in Begin Statement with AWK
I want to define an array in my BEGIN statement with undefin开发者_如何学运维ed index number; how can I do this in AWK?
BEGIN {send_packets_0to1 = 0;rcvd_packets_0to1=0;seqno=0;count=0; n_to_n_delay[];};
I have problem with n_to_n_delay[].
info gawk
says, in part:
Arrays in 'awk' superficially resemble arrays in other programming languages, but there are fundamental differences. In 'awk', it isn't necessary to specify the size of an array before starting to use it. Additionally, any number or string in 'awk', not just consecutive integers, may be used as an array index.
In most other languages, arrays must be "declared" before use, including a specification of how many elements or components they contain. In such languages, the declaration causes a contiguous block of memory to be allocated for that many elements. Usually, an index in the array must be a positive integer.
However, if you want to "declare" a variable as an array so referencing it later erroneously as a scalar produces an error, you can include this in your BEGIN
clause:
split("", n_to_n_delay)
which will create an empty array.
This can also be used to empty an existing array. While gawk
has the ability to use delete
for this, other versions of AWK do not.
I don't think you need to define arrays in awk. You just use them as in the example below:
{
if ($1 > max)
max = $1
arr[$1] = $0
}
END {
for (x = 1; x <= max; x++)
print arr[x]
}
Notice how there's no separate definition. The example is taken from The AWK Manual.
精彩评论