changeset 489:833865e835e5

Implement "array_clear()"
author Franz Glasner <fzglas.hg@dom66.de>
date Thu, 29 Aug 2024 19:44:19 +0200
parents d67efd0a34b5
children 39b771006a15
files share/local-bsdtools/array.sh
diffstat 1 files changed, 31 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- 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: