X-Git-Url: https://feistymeow.org/gitweb/?a=blobdiff_plain;f=scripts%2Ftesting%2Ftest_assoc_array.sh;fp=scripts%2Ftesting%2Ftest_assoc_array.sh;h=8da0424889b4a0e24e3ae0c117f2dd1fcb381c4c;hb=a5652986055dd00c3dd5f0ccdf4408aa484c51f8;hp=0000000000000000000000000000000000000000;hpb=b549c5ccea6b282f40add4dcefe1ef1a6865cbe6;p=feisty_meow.git diff --git a/scripts/testing/test_assoc_array.sh b/scripts/testing/test_assoc_array.sh new file mode 100644 index 00000000..8da04248 --- /dev/null +++ b/scripts/testing/test_assoc_array.sh @@ -0,0 +1,65 @@ +#!/bin/bash + +############## + +# simple examples... + +declare -A snuggles + # make an associative array + +snuggles=([book]=petunia [muffets]="glasgow robbery") + # keys: book and muffets + # values: (second part) + +snuggles+=([morgower]=flimshaw) + # adding entries to it. + +echo ${!snuggles[x]} + # show index x's key. + +echo ${snuggles[x]} + # show index x's value. + +############## + +# excellent code from: +# http://blog.spencertipping.com/2009/08/constant-time-associative-arrays-in-bash + +typeset -a table_keys +typeset -a table_values + +function index_of_key () { + initial=$(($(echo $1 | md5sum | cut -c 18-32 | awk '{print "0x"$1}'))) + while [[ ${table_keys[$initial]} && ${table_keys[$initial]} != $1 ]]; do + initial=$((initial + 1)) + done + echo -n $initial +} + +function associate () { + index=$(index_of_key $1) + table_keys[$index]=$1 + table_values[$index]=$2 + echo -n $2 +} + +function lookup () { + index=$(index_of_key $1) + echo -n ${table_values[$index]} +} + +echo Associating foo with bar and bif with baz +associate foo bar && echo +associate bif baz && echo + +echo -n Looking up foo: +lookup foo && echo + +echo -n Looking up bif: +lookup bif && echo + +echo -n Looking up bar: +lookup bar && echo + +############## +