如何在awk中初始化一个数组? [英] How to initialize an array of arrays in awk?
问题描述
是否可以在 AWK 中初始化这样的数组?
Is it possible to initialize an array like this in AWK ?
Colors[1] = ("Red", "Green", "Blue")
Colors[2] = ("Yellow", "Cyan", "Purple")
然后有一个二维数组,其中 Colors[2,3]="Purple".
And then to have a two dimensional array where Colors[2,3]="Purple".
来自 另一个线程 我知道这是不可能的(遗憾的是,有如果不滥用 split()" ,就无法一次设置一个数组.无论如何,我想 100% 确定,并且我确信还有其他人有同样的问题.
From another thread I understand that it's not possible ( "sadly, there is no way to set an array all at once without abusing split()" ). Anyways I want to be 100% sure and I'm sure that there are others with the same question.
我正在寻找初始化上述数组的最简单方法,如果能写得好,那就太好了.
I am looking for the easiest method to initialize arrays like the one above, will be nice to have it well written.
推荐答案
感谢您的回答.无论如何,对于那些想要初始化一维数组的人,这里有一个例子:
Thanks for the answers. Anyways, for those who want to initialize unidimensional arrays, here is an example:
SColors = "Red_Green_Blue"
split(SColors, Colors, "_")
print Colors[1] " - " Colors[2] " - " Colors[3]
这篇关于如何在awk中初始化一个数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!