Bash 将数组与其他数组的输入组合,尊重特定的循环顺序

Bash 将数组与其他数组的输入组合,尊重特定的循环顺序

我正在尝试生成一个新数组,结合其他数组,尊重特定的序列。在纯 Bash 中。例子:

numbers=(0 1 2 3 4 5 6 7 8);

colors=(red blue green);

loop_sequence=(numbers numbers colors numbers colors colors)

输出示例:

0 1 red 2 blue green 3 4 red 5 blue green 6 7 red 8 blue green 0 1 red 2 blue green...

但我发现很难进行迭代的内部循环。非常欢迎任何帮助。

答案1

动态变量名称在 bash 中很棘手,但可以通过变量间接命名。

numbers=(0 1 2 3 4 5 6 7 8)
colors=(red blue green)
loop_sequence=(numbers numbers colors numbers colors colors)

# keep track of where you are in each array
declare -A idx=([numbers]=0 [colors]=0)

for varname in "${loop_sequence[@]}"; do 
    tmp="${varname}[${idx[$varname]}]"   # construct the array reference
    echo "$varname => ${!tmp}"           # variable indirection gets the value
    (( idx[$varname]++ ))
done

输出

numbers => 0
numbers => 1
colors => red
numbers => 2
colors => blue
colors => green

要无限期地重复loop_sequence,您可以:

declare -A idx=([numbers]=0 [colors]=0)
# store the array sizes for convenience
declare -A size=([numbers]=${#numbers[@]} [colors]=${#colors[@]})
while true; do 
    for varname in "${loop_sequence[@]}"; do 
        tmp="${varname}[${idx[$varname]}]"
        echo "$varname => ${!tmp}"
        # loop the array index
        (( idx[$varname] = (idx[$varname]+1) % size[$varname] ))
    done
done | less

答案2

x=$(( (l=${#loop_sequence[@]}) * (n=${#numbers[@]}) * (c=${#colors[@]})))
_n= _c= _l=
set --    
math(){ return "$(($1-=-($1<${1#?})|$1-1))"; }
while   [ "$((x-=1))" -ge 0 ]
do      math _l
        case "${loop_sequence[$?-1]}" in
        n*)  math _n
             set -- "$@" "${numbers[$?-1]}";;
        *)   math _c
             set -- "$@" "${colors[$?-1]}";;
        esac
done;   printf %s\\n  "$@"

0
1
red
2
blue
green
3
4
red
5
blue
green
6
7
red
8
blue
green
0
1
red
2
blue
green
3
4
red
5
blue
green
6
7
red
8
blue
green
0
1
red
2
blue
green
3
4
red
5
blue
green
6
7
red
8
blue
green
0
1
red
2
blue
green
3
4
red
5
blue
green
6
7
red
8
blue
green
0
1
red
2
blue
green
3
4
red
5
blue
green
6
7
red
8
blue
green
0
1
red
2
blue
green
3
4
red
5
blue
green
6
7
red
8
blue
green
0
1
red
2
blue
green
3
4
red
5
blue
green
6
7
red
8
blue
green
0
1
red
2
blue
green
3
4
red
5
blue
green
6
7
red
8
blue
green
0
1
red
2
blue
green
3
4
red
5
blue
green
6
7
red
8
blue
green

相关内容