Listing 4: Associative arrays
subsep="_"
arrset() # array subscript value
{
array="$1"
shift
subscript="$1"
shift
eval [ \"\${$array$subsep$subscript+x}\" ] || \
eval $array${subsep}${subsep}names=\
\"\$$array${subsep}${subsep}names $subscript\"
eval $array$subsep$subscript=\"$*\"
}
arrget() # target array subscript
{
eval $1=\$$2$subsep$3
}
arrnames() # target array
{
eval $1=\"\$$2${subsep}${subsep}names\"
}
arrclear() # array
{
eval subscripts=\$$1${subsep}${subsep}names
for subscript in $subscripts
do
unset $1${subsep}$subscript
done
unset $1${subsep}${subsep}names
}
arrexist()
{
eval [ ! \"\$$1${subsep}${subsep}names\" ] || return 0
return 1
}
arrelemexist() # array subscript
{
eval [ ! \"\$$1${subsep}$2\" ] || return 0
return 1
}
arrelemclear() # array subscript
{
if arrelemexist $1 $2
then
unset $1${subsep}$2
eval ListDel $1${subsep}${subsep}names $2 \
\$$1${subsep}${subsep}names
fi
}
# End of File
|