# HG changeset patch # User Franz Glasner # Date 1725031760 -7200 # Node ID 562e630afb25741b8e88a627627c0ada2dfb978e # Parent 46406503a6357a86d88bd7c1e2f9059c641f3e25 Implement a debug output routine for arrays: array_debug() diff -r 46406503a635 -r 562e630afb25 share/local-bsdtools/array.sh --- a/share/local-bsdtools/array.sh Fri Aug 30 16:49:36 2024 +0200 +++ b/share/local-bsdtools/array.sh Fri Aug 30 17:29:20 2024 +0200 @@ -344,3 +344,45 @@ done return 0 } + + +#: +#: Print the contents of an array to stderr +#: +#: Args: +#: $1 (str): The name of an array. The array may exist or not. +#: +#: Returns: +#: 0 +#: +array_debug() { + local _name + + local _gvrname _l + + [ $# -lt 1 ] && _array_fatal "missing array name" + _name=$1 + _gvrname=${_farr_global_prefix}$1 + + # Check whether the variable already exists + eval _l=\${${_gvrname}__:-__UNSET__} + if [ "${_l}" = "__UNSET__" ]; then + echo "DEBUG: array \`${_name}' does not exist" 1>&2 + return 0 + fi + echo "DEBUG: array \`${_name}' has length ${_l}" 1>&2 + if [ ${_l} -gt 0 ]; then + echo "DEBUG: its contents:" 1>&2 + array_for_each ${_name} _array_debug_print_value + fi + return 0 +} + + +#: +#: Debug output helper for `array_debug` +#: +_array_debug_print_value() { + printf "DEBUG: idx: %s, val: \`%s'\\n" "$2" "$3" 1>&2 + return 0 +}