How to create, access and manipulate associative arrays?

2

I'm using GNU-Bash in my MingW terminal (% with%). I need to create a data dictionary to check if a new key was found and associate this new key with an index. For this, I think the best solution would be an associative array.

However, I can not make this array work!

For tests, I'm assigning incremental values to the keys, one key per line. For example, I expect to get the following key / value pairs for this entry:

./array_assoc_platonico.sh << EOL
> bash
> scripting
> is
> tought
> as
> bash
> is
> pretty
> EOL
bash:1
scripting:2
is:3
tought:4
as:5
pretty:6

The print does not matter to me, but the contents of my array should look something like this.

My script so far:

#!/bin/bash

NEXT_IDX=1
while read line; do
    if [ "x${chaves[$line]}" = "x" ]; then
        # então chave nova
        chaves[$line]=$NEXT_IDX
        NEXT_IDX=$(( NEXT_IDX + 1 ))

        echo "$line:${chaves[$line]}"
    fi
done

However, my output is:

./array_assoc_falho.sh << EOL
> bash
> scripting
> is
> tought
> as
> bash
> is
> pretty
> EOL
bash:1

When I give bash --version: GNU bash, version 4.4.12(1)-release (i686-pc-msys) at the end of the reading, I get the following:

declare -a chaves=([0]="1")

Where am I wrong to use the associative array in bash?

    
asked by anonymous 26.01.2018 / 04:17

1 answer

3

The correct one is declare -A chaves , the variable "keys" will be treated as an array.

#!/bin/bash
declare -A chaves
NEXT_IDX=1
while read line; do
    if [ "x${chaves[$line]}" = "x" ]; then
        # então chave nova
        chaves[$line]=$NEXT_IDX
        NEXT_IDX=$(( NEXT_IDX + 1 ))

        echo "$line:${chaves[$line]}"
    fi
done

Output:

bash:1
scripting:2
is:3
tought:4
as:5
pretty:6

Variables in Bash

  • When you enter the parameter -a you create an indexed array , that is, a variable containing a list where indexes are numbers.

    #!/bin/bash
    declare -a CARROS=("Gol" "Argo" "C3" "Saveiro")
    for ((I=0;I<3;I++)); do
        echo $I ${CARROS[I]};
    done
    

    The output will be:

    0 Gol
    1 Argo
    2 C3
    
  • The parameter -A has its operation equal to the indexed array , the difference is in using a string as the key rather than numeric index.

  • The parameter -i defines the variable as an integer

    $ declare -i NUMERO=2018
    $ echo ${NUMERO}
    2018
    $ NUMERO+=2
    $ echo ${NUMERO}
    2020
    $ NUMERO="UM NOME QUALQUER" # IRÁ RETORNAR ZERO
    0
    
  • The parameters -l and -u are used to convert string to lowercase and upper case.

    $ declare -l SITE="Pt.StaCkOverFlow.com"
    $ echo ${SITE}
    pt.stackoverflow.com
    $ declare -u SITE="pt.stackoverflow.com"
    $ echo ${SITE}
    PT.STACKOVERFLOW.COM
    
  • The parameter -r makes the variable read-only.

    $ declare -r VAR="Minha variável"
    $ echo ${VAR}
    Minha variável
    $ VAR="Novo conteúdo" # FOI DECLARADA COMO LEITURA, RETORNARA ERRO
    ./teste.sh: line 5: VAR: a variável permite somente leitura
    
  • The -p parameter is used to display the attributes and values of a variable.

    $ declare -a VAR=("Corsa" "Gol" "Palio" "Uno")
    $ declare -p VAR
    declare -a VAR='([0]="Corsa" [1]="Gol" [2]="Palio" [3]="Uno")'
    
26.01.2018 / 04:45