15

I'm trying to read the English dictionary in Linux into an associative array, using the words as keys and and predefined string as a value. This way I can look up words by key to see if they exist. Also I need all to words to be lowercase. It's fairly simple but the bash syntax is getting in my way. When I run the code below, I get a 'bad array subscript' error. Any thoughts as to why that might be ?

 function createArrayFromEnglishDictionary(){
        IFS=$'\n'
        while read -d $'\n' line; do
            #Read string into variable and put into lowercase.
            index=`echo ${line,,}`
            englishDictionaryArray[$index]="exists"
            done < /usr/share/dict/words
            IFS=$' \t\n'
    }
Asgeir
  • 727
  • 3
  • 9
  • 20

4 Answers4

6

I think following example will help..

$ declare -A colour
$ colour[elephant]="black"
$ echo ${colour[elephant]}
black

$ index=elephant
$ echo ${colour["$index"]}
black
Jobin
  • 6,506
  • 5
  • 24
  • 26
5

$index is empty at some point. You also have a rather totally pointless use of echo assuming you wanted the line verbatim and not whitespace compressed. Just use index="${line,,}".

ribbons
  • 31
  • 6
jørgensen
  • 10,149
  • 2
  • 20
  • 27
  • index="${line,,}" didn't work, but I don't know why. I had tried this before and expected it to work. Should the key-value assignment be any different from what it is like now ? – Asgeir Mar 18 '12 at 21:15
  • It seems to work now, I changed englishDictionaryArray[$index]="exists" to englishDictionaryArray["${index}"]="exists" at first, that didn't work. And of course, the error was completely unintuitive, I had to add spaces at both sides of ". so [ "${index}" ] works. Man I hate bash sometimes. – Asgeir Mar 18 '12 at 21:22
  • So don't use bash for these things. There are many other, more suitable scripting languages for this kind of advanced string juggling-and-manipulation. – jørgensen Mar 19 '12 at 18:05
3

To use associative arrays in bash, you need bash version 4, and you need to declare the array as an associative array with declare -A englishDictionaryArray

http://www.linuxjournal.com/content/bash-associative-arrays

glenn jackman
  • 238,783
  • 38
  • 220
  • 352
1

Combining your work and other answers, try this:

I'm using GNU bash, version 4.2.37(1)-release (x86_64-pc-linux-gnu)

#!/bin/bash
declare -A DICT
function createDict(){
    while read LINE; do
        INDEX=${LINE,,}
        DICT[$INDEX]="exists"
    done < /usr/share/dict/words
}

createDict

echo ${DICT[hello]}
echo ${DICT[acdfg]}
echo ${DICT["a's"]}
philcolbourn
  • 4,042
  • 3
  • 28
  • 33
  • @Daniel Kuta, 6 years later and it still seems to work for me. This does not help you, but perhaps you can provide error messages and output and version of bash? – philcolbourn Apr 12 '19 at 13:28
  • 2
    Note that `/bin/bash` on macOS is bash version 3.2, which does not support associative arrays. Mac users could install and use latest bash from e.g. homebrew. – fornwall Oct 29 '19 at 16:42
  • 2
    For those who don't know `${LINE,,}` will return a lowercase version of `$LINE` – Yzmir Ramirez Dec 27 '20 at 01:25