如何保持关联数组顺序? [英] How to keep associative array order?

查看:89
本文介绍了如何保持关联数组顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试遍历Bash中的关联数组.

I try to iterate over an associative array in Bash.

这似乎很简单,但是循环并没有遵循数组的初始顺序.

It seems to be simple, but the loop doesn't follow the initial order of the array.

这是一个简单的脚本可以尝试:

Here is a simple script to try:

#!/bin/bash

echo -e "Workspace\n----------";
lsb_release -a

echo -e "\nBash version\n----------";
echo -e $BASH_VERSION."\n";

declare -A groups;
groups["group1"]="123";
groups["group2"]="456";
groups["group3"]="789";
groups["group4"]="abc";
groups["group5"]="def";

echo -e "Result\n----------";
for i in "${!groups[@]}"
do
    echo "$i => ${groups[$i]}";
done

输出:

Workspace
----------
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 14.04.2 LTS
Release:    14.04
Codename:   trusty

Bash version
----------
4.3.11(1)-release.

Result
----------
group3 => 789
group2 => 456
group1 => 123
group5 => def
group4 => abc

为什么我没有group1group2等?

我不想按字母顺序排列,我只希望循环遵循数组的初始声明顺序...

I don't want to have an alphanum order, I just want that the loop follow the initial declaration's order of the array...

有办法吗?

推荐答案

正如已经指出的那样,没有错误.关联数组以哈希"顺序存储.如果要订购,则不要使用关联数组.或者,您可以使用非关联数组,也可以使用关联数组.

As already pointed out, there is no mistake. Associative arrays are stored in a 'hash' order. If you want ordering, you don't use associative arrays. Or, you use a non-associative array as well as an associative array.

保留第二个(非关联性)数组,该数组按键的创建顺序对其进行标识.然后逐步浏览第二个数组,在打印数据时使用其内容为第一个(关联)数组设置键.像这样:

Keep a second (non-associative) array that identifies the keys in the order that they're created. Then step through the second array, using its contents to key the first (associative) array when printing the data. Like this:

declare -A groups;      declare -a orders;
groups["group1"]="123"; orders+=( "group1" )
groups["group2"]="456"; orders+=( "group2" )
groups["group3"]="789"; orders+=( "group3" )
groups["group4"]="abc"; orders+=( "group4" )
groups["group5"]="def"; orders+=( "group5" )

# Convoluted option 1
for i in "${!orders[@]}"
do
    echo "${orders[$i]}: ${groups[${orders[$i]}]}"
done
echo

# Convoluted option 1 - 'explained'
for i in "${!orders[@]}"
do
    echo "$i: ${orders[$i]}: ${groups[${orders[$i]}]}"
done
echo

# Simpler option 2 - thanks, PesaThe
for i in "${orders[@]}"
do
    echo "$i: ${groups[$i]}"
done

更简单的选项2"由 PesaThe 建议. com/questions/29161323/how-to-keep-asociate-associative-array-order-in-bash/29161460?noredirect = 1#comment82897599_29161460>评论,应优先使用卷积选项".

The 'simpler option 2' was suggested by PesaThe in a comment, and should be used in preference to the 'convoluted option'.

示例输出:

group1: 123
group2: 456
group3: 789
group4: abc
group5: def

0: group1: 123
1: group2: 456
2: group3: 789
3: group4: abc
4: group5: def

group1: 123
group2: 456
group3: 789
group4: abc
group5: def

您可能不想像这样每行有两个语句,但是它强调了两个数组的处理之间的并行性.

You probably don't want to have two statements per line like that, but it emphasizes the parallelism between the handling of the two arrays.

在问题中分配作业后的分号并不是真正必要的(尽管它们并没有造成积极的伤害,除了让读者怀疑为什么").

The semicolons after the assignments in the question are not really necessary (though they do no active harm, beyond leaving the reader wondering 'why?').

这篇关于如何保持关联数组顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆