简体   繁体   中英

Displaying bash array

#!/bin/bash
declare -a array
printf 'Matrix size:' ;
read n;
printf '\n';
      for ((i=1;i<=n;i++))
        do
      for((j=1;j<=n;j++))
        do
          printf 'x[%d][%d]=' ${array[i][j]};
          read array[i][j];
     done
     done
     echo "Initial matrix:"
        for((i=1;i<=$n;i++))
          do 
        for ((j=1;j<=n;j++))
          do
       printf '%d' ${array[i][j]};
           printf '\n';
        done
        done

A can't display bash array and don't understand where is my mistake. For example I have: n=3....I enter numbers in array (1-9) When I displaying: Initial matrix: 3 3 3 6 6 6 9 9 9 Thx

bash doesn't have multidimensional arrays; when you reference array[i][j] , the [j] is ignored. bash also has other limitations that tend to make it unsuitable for this sort of thing, such as not supporting floating point math (natively, anyway).

If you need to fake a multidimensional array in bash, you can fake it by using array[i*n+j] to store array[i][j]:

#!/bin/bash
declare -a array
read -p 'Matrix size: ' n
for ((i=1; i<=n; i++)); do
    for ((j=1; j<=n; j++)); do
        read -p "x[$i][$j]=" array[i*n+j]
    done
done

echo "Initial matrix:"
for ((i=1; i<=n; i++)); do 
    for ((j=1; j<=n; j++)); do
        printf '%d ' ${array[i*n+j]}
    done
    printf '\n'
done

Note that I've done some additional cleanup on your code:

  • Don't put semicolons at the end of a line, they're redundant in a shell script.
  • Print prompts with read -p instead of printf .
  • The input loop was printing array[i][j] (ie the array's contents) where it should've printed i and j.
  • You don't need to use printf to interpolate variables in a string, just embed $i in a double-quoted string (or use ${i} to avoid ambiguity about where the end of the variable name is).
  • Finally, when printing the array I made it put a newline after each row, rather than after each element.

It's possible to use bash 4's associative arrays to get multidimensional arrays... sorta:

#!/bin/bash

declare -A array

printf "Matrix size: "
read -r n
for ((i = 0; i < n; i++)); do
    for ((j = 0; j < n; j++)); do
        printf "array[$i][$j] = "
        read -r val
        array["${i}_${j}"]=$val
    done
done

echo "Initial matrix:"
for key in "${!array[@]}"; do
    val=${array[$key]}
    echo "$key $val"
done

However, there is no true multidimensional array support available in bash.

Unlike the answer using standard numerically-indexed arrays, this approach doesn't require the dimensions of the array to be known before it can be read.

All of Gordon's remarks are absolutely correct, for the sake of completeness I'll just add that ksh93 (I am not certain about ksh88 ) does support multidimensional arrays (but it's a notoriously un(der)documented feature), so you can do this natively with ksh :

matrix.sh :

#!/bin/ksh

printf "Matrix size: "
read n
for ((i = 0; i < n; i++)); do
        for ((j = 0; j < n; j++)); do
                printf "array[$i][$j] = "
                read array[i][j]
        done
done

echo "Initial matrix:"
for ((i = 0; i < ${#array[@]}; i++)); do
        for k in "${array[i][@]}"; do
                printf "${k} "
        done
        echo
done

Example:

$ ./matrix.sh
Matrix size: 2
array[0][0] = 3
array[0][1] = 4
array[1][0] = 5
array[1][1] = 6
Initial matrix:
3 4
5 6

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM