17

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'
    }
3
  • IFS=$' \t\n' is the same as unset IFS. Commented Mar 19, 2012 at 2:36
  • What is the syntax ${VAR,,}? I've never seen this before, and can't find any references elsewhere Commented Oct 4, 2017 at 8:00
  • <code>${VAR,,}</code> means lowercase in Bash. Commented Dec 6, 2017 at 0:28

4 Answers 4

7

I think following example will help..

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

$ index=elephant
$ echo ${colour["$index"]}
black
Sign up to request clarification or add additional context in comments.

Comments

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,,}".

3 Comments

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 ?
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.
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.
2

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

Comments

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"]}

3 Comments

@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?
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.
For those who don't know ${LINE,,} will return a lowercase version of $LINE

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.