Home > Net >  declare -a/-A matrix (Bash)
declare -a/-A matrix (Bash)

Time:11-03

What is the difference between declare -a matrix and declare -A matrix in bash programming?

declare -A matrix
declare -a res

read_matrix() {
    local i=0
    local line
    local j
    while read -r line; do
        j=0
        IFS=,
        for v in $(echo "$line")
        do
            matrix[$i,$j]="$v"
            j=$((j 1))
        done
        i=$((i 1))
    done
}

This is a piece from a bash script that calculates the solution of systems of linear equations using the Gaussian elimination.

CodePudding user response:

declare -A matrix creates an associative array: Its keys are arbitrary strings. This is a feature that was added in bash 4.0 and is not available in prior releases (such as the bash 3.2 that Apple ships), and is why you can have a comma inside your keys.

declare -a matrix creates an indexed array: Its keys are interpreted as numbers.

  • Related