数组 – asort(src,dest)到多维数组

数组 – asort(src,dest)到多维数组,第1张

概述我试图滥用asort()(只是因为)将数组src复制到数组dest,没问题: $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 我试图滥用asort()(只是因为)将数组src复制到数组dest,没问题:
$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)到多维数组所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/yw/1049871.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存