8 # make an associative array
10 snuggles=([book]=petunia [muffets]="glasgow robbery")
11 # keys: book and muffets
12 # values: (second part)
14 snuggles+=([morgower]=flimshaw)
15 # adding entries to it.
21 # show index x's value.
25 # excellent code from:
26 # http://blog.spencertipping.com/2009/08/constant-time-associative-arrays-in-bash
29 typeset -a table_values
31 function index_of_key () {
32 initial=$(($(echo $1 | md5sum | cut -c 18-32 | awk '{print "0x"$1}')))
33 while [[ ${table_keys[$initial]} && ${table_keys[$initial]} != $1 ]]; do
34 initial=$((initial + 1))
39 function associate () {
40 index=$(index_of_key $1)
42 table_values[$index]=$2
47 index=$(index_of_key $1)
48 echo -n ${table_values[$index]}
51 echo Associating foo with bar and bif with baz
52 associate foo bar && echo
53 associate bif baz && echo
55 echo -n Looking up foo:
58 echo -n Looking up bif:
61 echo -n Looking up bar: