Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bad array subscript error in associative array

I am trying to create a dictionary program in Bash with the following options : 1. Add a word 2. Update meaning 3. Print dictionary 4. Search a word 5. Search by keyword

For the same, I am creating 2 associative arrays, 1 to store the word - meaning and other to store words-keyword.

The problem is I am not able to store values in the array. Everytime I try to do it, it gives me an error dict[$word]: bad array subscript

Here is the code for part 1

echo
echo -n "Enter a word : "
read $word
echo
echo -n "Enter it's meaning : "
read $meaning
echo
echo -n "Enter some keywords(with space in between) to describe the word : "
read $keyword

dict[$word]=$meaning
keywords[$word]=$keyword
;;

I also tried inserting the following code to remove new line as suggested in some posts but ended up with same result.

word=`echo $word | grep -s '\n'`
keyword=`echo $keyword | grep -s '\n'`

Have also tried the following way :

dict["$word"]="$meaning"
keywords["$word"]="$keyword"
;;

Output : dict[$word]: bad array subscript

like image 923
Akshay Avatar asked Sep 04 '17 17:09

Akshay


2 Answers

When reading a variable you preface the variable name with a $ (or wrap in $( and )).

When assigning a value to a variable you do not preface the variable name with a $.

In your example your 3x echo/read sessions are attempting to assign values to your variables, but you've prefaced your variables with $, which means your variables are not getting set as you expect; this in turn could be generating your error because $word is not set/defined (depends on version of bash).

You should be able to see what I mean with the following code snippet:

unset word
echo
echo -n "Enter a word : "
read $word
echo ".${word}."

What do you get as ouput? .. ? .<whatever_you_typed_in>. ?

You may also have a problem with your associative arrays (depending on bash version); as George has mentioned, you should play it safe and explicitly declare your associative arrays.

I would suggest editing your input script like such (remove leading $ on your read variables; explicitly declaring your associative arrays):

echo
echo -n "Enter a word : "
read word
echo
echo -n "Enter it's meaning : "
read meaning
echo
echo -n "Enter some keywords(with space in between) to describe the word : "
read keyword

# print some debug messages:
echo "word=.${word}."
echo "meaning=.${meaning}."
echo "keyword=.${keyword}."

# declare arrays as associative
declare -A dict keywords

# assign values to arrays
dict[$word]=$meaning
keywords[$word]=$keyword

# verify array indexes and values
echo "dict index(es)    : ${!dict[@]}"
echo "dict value(s)     : ${dict[@]}"
echo "keywords index(es): ${!keywords[@]}"
echo "keywords value(s) : ${keywords[@]}"
like image 70
markp-fuso Avatar answered Sep 28 '22 14:09

markp-fuso


In my bash 4.4 , this is not raising any error but is not working correctly either:

$ w="one";m="two";d["$w"]="$m";declare -p d
declare -a d=([0]="two")

It is obvious that bash determines array d as a normal array and not as an associative array.

On the contrary, this one works fine:

$ w="one";m="two";declare -A d;d["$w"]="$m";declare -p d
declare -A d=([one]="two" )

According to bash manual, you need first to declare -A an array in order to be used as an associative one.

like image 35
George Vasiliou Avatar answered Sep 28 '22 15:09

George Vasiliou