# HG changeset patch # User Franz Glasner # Date 1724953459 -7200 # Node ID 833865e835e5fa9acab3e17db76146497c729ee3 # Parent d67efd0a34b544bca55ba2b2859444c4e7ec27b9 Implement "array_clear()" diff -r d67efd0a34b5 -r 833865e835e5 share/local-bsdtools/array.sh --- a/share/local-bsdtools/array.sh Thu Aug 29 19:25:27 2024 +0200 +++ b/share/local-bsdtools/array.sh Thu Aug 29 19:44:19 2024 +0200 @@ -190,6 +190,37 @@ #: +#: Empty an existing array +#: +#: Args: +#: $1 (str): The name of the existing array +#: +array_clear() { + local _name + + local _l _idx + + [ $# -lt 1 ] && _array_fatal "missing array name" + _name=$1 + + # Check whether the variable already exists + eval _l=\${${_name}__:-__UNSET__} + if [ "${_l}" = "__UNSET__" ]; then + _array_fatal "array \`${_name}' does not exist" + fi + + _idx=1 + while [ ${_idx} -le ${_l} ]; do + eval unset ${_name}_${_idx} + _idx=$((${_idx} + 1)) + done + + # Length is now zero + eval ${_name}__=0 +} + + +#: #: Destroy and unset an array and all its elements #: #: Args: