是否可以在不指定索引的情况下将项目附加到awk中的数组? [英] Is it possible to append an item to an array in awk without specifying an index?

查看:76
本文介绍了是否可以在不指定索引的情况下将项目附加到awk中的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我意识到awk具有关联数组,但是我想知道是否有awk与此等效:

I realize that awk has associative arrays, but I wonder if there is an awk equivalent to this:

http://php.net/manual/en/function.array- push.php

显而易见的解决方法是说:

The obvious workaround is to just say:

array[$new_element] = $new_element

但是,这似乎比所需的要容易理解,而且更容易破解.

However, this seems less readable and more hackish than it needs to be.

推荐答案

我认为awk中不会立即提供数组长度(至少在我弄弄的版本中不是).但是您可以简单地保持长度,然后执行以下操作:

I don't think an array length is immediately available in awk (at least not in the versions I fiddle around with). But you could simply maintain the length and then do something like this:

array[arraylen++] = $0;

然后通过相同的整数值访问元素:

And then access the elements it via the same integer values:

for ( i = 0; i < arraylen; i++ )
   print array[i];

这篇关于是否可以在不指定索引的情况下将项目附加到awk中的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆