2
I am using GNU-Bash on my Mingw terminal (bash --version: GNU bash, version 4.4.12(1)-release (i686-pc-msys)
). I need to create a data dictionary to check if a new key has been found and associate this new key with an index. For this, I believe that the best solution would be an associative array.
However, I’m not getting this array to work!
For testing, I’m assigning incremental values to keys, one key per line. For example, I hope to get the following key/value pairs for that 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
I don’t care about printing, but the contents of my array should be 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 obtained output is being:
./array_assoc_falho.sh << EOL
> bash
> scripting
> is
> tought
> as
> bash
> is
> pretty
> EOL
bash:1
When I give one declare -p chaves
at the end of the reading, I get the following:
declare -a chaves=([0]="1")
Where I am missing the use of the associative array in bash?
That’s the intention. I put in the first section what the terminal would print. But note that I need to manipulate the associative array, not just format the output
– Jefferson Quesado
I’m the one who mosqued :)
– NoobSaibot