gpt4 book ai didi

awk - 如何使用 AWK 在 Begin 语句中定义动态数组

转载 作者:行者123 更新时间:2023-12-04 00:36:48 24 4
gpt4 key购买 nike

我想在我的 BEGIN 语句中定义一个带有未定义索引号的数组;我怎样才能在 AWK 中做到这一点?

BEGIN {send_packets_0to1 = 0;rcvd_packets_0to1=0;seqno=0;count=0; n_to_n_delay[];}; 

我对 n_to_n_delay[] 有问题。

最佳答案

info gawk部分说:

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.



但是,如果您想将变量“声明”为数组,以便稍后将其作为标量错误地引用会产生错误,您可以将其包含在您的 BEGIN 中。条款:
split("", n_to_n_delay)

这将创建一个空数组。

这也可用于清空现有数组。虽然 gawk有能力使用 delete为此,其他版本的 AWK 没有。

关于awk - 如何使用 AWK 在 Begin 语句中定义动态数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4353151/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com