bash脚本中的第二个数组有什么问题? - 二维数组

时间:2015-07-16 23:07:11

标签: bash

#!/bin/bash
declare -a matrix
num_rows=4
num_columns=5

for ((i=1;i<=num_rows;i++)) do
    for ((j=1;j<=num_columns;j++)) do
        matrix[$i,$j]=$j
    done
done
f1="%$((${#num_rows}+1))s"
f2=" %9s"

printf "$f1" ''
for ((i=1;i<=num_rows;i++)) do
printf "$f2" $i
done
echo

for ((j=1;j<=num_columns;j++)) do
    printf "$f1" $j
    for ((i=1;i<=num_rows;i++)) do
        printf "$f2" ${matrix[$i,$j]}
    done
    echo
done
=> run result:
           1         2         3         4   
 1         1         1         1         1
 2         2         2         2         2
 3         3         3         3         3
 4         4         4         4         4
 5         5         5         5         5

========================     #!/斌/庆典     声明-a矩阵     NUM_ROWS = 4     NUM_COLUMNS = 5

for ((i=1;i<=num_rows;i++)) do
    for ((j=1;j<=num_columns;j++)) do
        matrix[$i,$j]=$i
    done
done
f1="%$((${#num_rows}+1))s"
f2=" %9s"

printf "$f1" ''
for ((i=1;i<=num_rows;i++)) do
printf "$f2" $i
done
echo

for ((j=1;j<=num_columns;j++)) do
    printf "$f1" $j
    for ((i=1;i<=num_rows;i++)) do
        printf "$f2" ${matrix[$i,$j]}
    done
    echo
done

如果将数组的赋值从$ j更改为$ i 结果:

           1         2         3         4
 1         4         4         4         4
 2         4         4         4         4
 3         4         4         4         4
 4         4         4         4         4
 5         4         4         4         4

这似乎是因为bash不支持多维数组?

有没有办法在bash中使用二维数组?

0 个答案:

没有答案