gpt4 book ai didi

bash - awk 排序多维数组

转载 作者:行者123 更新时间:2023-11-29 09:26:28 24 4
gpt4 key购买 nike

<分区>

GNU awk 支持 multidimensional arrays :

q[1][1] = "dog"
q[1][2] = 999
q[2][1] = "mouse"
q[2][2] = 777
q[3][1] = "bird"
q[3][2] = 888

我想对 q 的“第二列”进行排序,这样我就剩下:

q[1][1] = "mouse"
q[1][2] = 777
q[2][1] = "bird"
q[2][2] = 888
q[3][1] = "dog"
q[3][2] = 999

如您所见,“第一列”值移动到与第二列保持一致。我懂了GNU Awk 提供了一个 asort function但它似乎不支持多维数组。如果有帮助,这是一个 working Ruby example :

q = [["dog", 999], ["mouse", 777], ["bird", 888]]
q.sort_by{|z|z[1]}
=> [["mouse", 777], ["bird", 888], ["dog", 999]]

我最终使用了一个常规数组,然后用换行符分隔重复项:

q[777] = "mouse"
q[999] = "dog" RS "fish"
q[888] = "bird"
for (z in q) {
print q[z]
}

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