Task

The goal is to create an associative array (also known as a dictionary, map, or hash).



Related tasks:

 Associative arrays/Iteration
 Hash from two arrays


See also

 Array
 Associative array: Creation, Iteration
 Collections
 Compound data type
 Doubly-linked list: Definition, Element definition, Element insertion, List Traversal, Element Removal
 Linked list
 Queue: Definition, Usage
 Set
 Singly-linked list: Element definition, Element insertion, List Traversal, Element Removal
 Stack


map='p.map'
 
function init() {
cat <<EOF > $map
apple a
boy b
cat c
dog d
elephant e
EOF
}
 
function put() {
    k=$1; v=$2;
    del $k
    echo $v $k >> $map
 }
 
function get() {
    k=$1
    for v in $(cat $map | grep "$k$"); do
        echo $v
        break
    done
 }
 
function del() {
    k=$1
    temp=$(mktemp)
    mv $map $temp
    cat $temp | grep -v "$k$" > $map
}
 
function dump() {
    echo "-- Dump begin --"
    cat $map
    echo "-- Dump complete --"
}
 
init
get c
put c cow
get c
dump