$awk 'BEGIN { split("first;second;third",src,";") # make src array for testing asort(src,dest,"@ind_num_asc") # copy array to dest for(i in dest) print i,src[i],dest[i] # output}'1 first first2 second second3 third third
但有没有办法使用多维数组作为dest数组?就像是:
asort(src,dest [src [1]],“@ ind_num_asc”)#或dest [src [1]] []
(前者产生的第二个参数不是数组,后面的语法错误
实际上,拆分的第一个参数是0美元,而我正试图将记录分组.)
当然我可以使用for循环,但我的大脑仍然坚持测试这个解决方案.
解决方法 你只需要在dest [src [1]]下创建一个数组,所以gawk知道dest [src [1]]是一个数组数组而不是默认的字符串数组:$cat tst.awkBEGIN { split("first;second;third",/;/) # make src array for testing asort(src,dest1d) # copy array to dest1d for(i in dest1d) print i,dest1d[i] # output print "" dest2d[src[1]][1] asort(src,dest2d[src[1]]) # copy array to dest2d for(i in dest2d) for (j in dest2d[i]) print i,j,dest2d[i][j] # output}$gawk -f tst.awk1 first first2 second second3 third thirdfirst 1 firstfirst 2 secondfirst 3 third
你给初始子数组的索引并不重要,因为它会被asort()删除.请参阅https://www.gnu.org/software/gawk/manual/gawk.html#Arrays-of-Arrays下的最后一个示例:
总结Recall that a reference to an uninitialized array element yIElds a
value of “”,the null string. This has one important implication when
you intend to use a subarray as an argument to a function,as
illustrated by the following example:06001
The way to work around this is to first force b[1] to be an array by
creating an arbitrary index:06002
以上是内存溢出为你收集整理的数组 – asort(src,dest)到多维数组全部内容,希望文章能够帮你解决数组 – asort(src,dest)到多维数组所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)