awk 中的关联数组排序

发布于 2024-08-25 08:51:22 字数 279 浏览 3 评论 0原文

我在 awk 中有一个关联数组,它的填充方式如下:

chr_count[$3]++

当我尝试打印我的 chr_counts 时,我使用这个:

for (i in chr_count) {
    print i,":",chr_count[i];
}

但毫不奇怪, i 的顺序没有以任何方式排序。 有没有一种简单的方法来迭代 chr_count 的排序键?

I have an associative array in awk that gets populated like this:

chr_count[$3]++

When I try to print my chr_counts, I use this:

for (i in chr_count) {
    print i,":",chr_count[i];
}

But not surprisingly, the order of i is not sorted in any way.
Is there an easy way to iterate over the sorted keys of chr_count?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

贱人配狗天长地久 2024-09-01 08:51:22

而不是 asort,请使用 asorti(source, target) 它将索引排序到一个新数组中,并且您不必复制该数组。

然后,您可以使用目标数组作为指向源数组的指针。

对于您的示例,您将像这样使用它:

n=asorti(chr_count, sorted)
for (i=1; i<=n; i++) {
        print sorted[i] " : " chr_count[sorted[i]]
}

Instead of asort, use asorti(source, destination) which sorts the indices into a new array and you won't have to copy the array.

Then you can use the destination array as pointers into the source array.

For your example, you would use it like this:

n=asorti(chr_count, sorted)
for (i=1; i<=n; i++) {
        print sorted[i] " : " chr_count[sorted[i]]
}
凉宸 2024-09-01 08:51:22

您可以使用排序命令。例如

for ( i in data )
 print i ":", data[i]  | "sort"

you can use the sort command. e.g.

for ( i in data )
 print i ":", data[i]  | "sort"
幸福%小乖 2024-09-01 08:51:22

我最近遇到了这个问题,发现使用 gawk 我可以设置 PROCINFO["sorted_in"] 的值来控制迭代顺序。我通过在线搜索 PROCINFO 找到了有效值的列表,并登陆了此 GNU Awk 用户指南页面: https://www.gnu.org/software/gawk/manual/html_node/Controlling-Scanning.html

这列出了 @{ind|val 形式的选项}_{num|type|str}_{asc|desc} 其中:

  • ind 按键(索引)排序,val 按值排序。
  • num 按数字排序,str 按字符串排序,type 按分配的类型排序。
  • asc 表示升序,desc 表示降序。

我只是使用:

PROCINFO["sorted_in"] = "@val_num_desc"
for (i in map) print i, map[i]

并且输出按值的降序排序。

I recently came across this issue and found that with gawk I could set the value of PROCINFO["sorted_in"] to control iteration order. I found a list of valid values for this by searching for PROCINFO online and landed on this GNU Awk User's Guide page: https://www.gnu.org/software/gawk/manual/html_node/Controlling-Scanning.html

This lists options of the form @{ind|val}_{num|type|str}_{asc|desc} with:

  • ind sorting by key (index) and val sorting by value.
  • num sorting numerically, str by string and type by assigned type.
  • asc for ascending order and desc for descending order.

I simply used:

PROCINFO["sorted_in"] = "@val_num_desc"
for (i in map) print i, map[i]

And the output was sorted in descending order of values.

翻了热茶 2024-09-01 08:51:22

请注意,asort()asorti() 是 gawk 特有的,awk 不知道。对于普通 awk,您可以推出自己的 sort() 或从其他地方获取一个。

Note that asort() and asorti() are specific to gawk, and are unknown to awk. For plain awk, you can roll your own sort() or get one from elsewhere.

梦毁影碎の 2024-09-01 08:51:22

这直接取自文档

 populate the array data
 # copy indices
 j = 1
 for (i in data) {
     ind[j] = i    # index value becomes element value
     j++
 }
 n = asort(ind)    # index values are now sorted
 for (i = 1; i <= n; i++) {
     do something with ind[i]           Work with sorted indices directly
     ...
     do something with data[ind[i]]     Access original array via sorted indices
 }

This is taken directly from the documentation:

 populate the array data
 # copy indices
 j = 1
 for (i in data) {
     ind[j] = i    # index value becomes element value
     j++
 }
 n = asort(ind)    # index values are now sorted
 for (i = 1; i <= n; i++) {
     do something with ind[i]           Work with sorted indices directly
     ...
     do something with data[ind[i]]     Access original array via sorted indices
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文