changeset 746:7e2279d6db0f

farray.sh: falist_get() and falist_tryget() now optionally can store the index of the found item into a variable also
author Franz Glasner <fzglas.hg@dom66.de>
date Tue, 08 Oct 2024 18:11:24 +0200
parents a5918c859fe9
children 6787e216285e
files share/local-bsdtools/farray.sh tests/farray-alist.t
diffstat 2 files changed, 23 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/share/local-bsdtools/farray.sh	Tue Oct 08 17:55:23 2024 +0200
+++ b/share/local-bsdtools/farray.sh	Tue Oct 08 18:11:24 2024 +0200
@@ -2416,6 +2416,8 @@
 #:   $1 (str): The name of the variable to put the value into.
 #:   $2 (str): The name of an existing alist.
 #:   $3: The key.
+#:   $4 (str, optional): The name of a variable where to store the index
+#:                       of the found item into.
 #:
 #: Exit:
 #:   - If the key is not found.
@@ -2423,7 +2425,7 @@
 #:     or if an internal inconsistency will be detected.
 #:
 falist_get() {
-    local __farr_varname __farr_name __farr_key
+    local __farr_varname __farr_name __farr_key __farr_idx_varname
 
     local __farr_token __farr_objname __farr_keyname __farr_valname __farr_len
     local __farr_idx __farr_getkey
@@ -2435,6 +2437,7 @@
     _farr_alist_get_meta "$@"
     [ $# -lt 2 ] && _farr_fatal "missing key"
     __farr_key="$2"
+    __farr_idx_varname="${3-}"
 
     __farr_idx=1
     while [ ${__farr_idx} -le ${__farr_len} ]; do
@@ -2445,6 +2448,7 @@
                 eval __farr_get_value=\"\$\{${__farr_valname}_${__farr_idx}\}\"
                 _farr_acquire_object "${__farr_get_value}"
                 eval "${__farr_varname}"=\"\$\{__farr_get_value\}\"
+                [ -n "${__farr_idx_varname}" ] && eval "${__farr_idx_varname}"=${__farr_idx}
                 return 0
             fi
         else
@@ -2464,13 +2468,15 @@
 #:   $1 (str): The name of the variable where to put the value into.
 #:   $2 (str): The name of an existing alist.
 #:   $3: The key.
+#:   $4 (str, optional): The name of a variable where to store the index
+#:                       of the found item into.
 #:
 #: Returns:
 #:   0 (truthy) on success,
 #:   1 (falsy) if the given key is not found.
 #:
 falist_tryget() {
-    local __farr_varname __farr_name __farr_key
+    local __farr_varname __farr_name __farr_key __farr_idx_varname
 
     local __farr_token __farr_objname __farr_keyname __farr_valname __farr_len
     local __farr_idx __farr_getkey
@@ -2482,6 +2488,7 @@
     _farr_alist_get_meta "$@"
     [ $# -lt 2 ] && _farr_fatal "missing key"
     __farr_key="$2"
+    __farr_idx_varname="${3-}"
 
     __farr_idx=1
     while [ ${__farr_idx} -le ${__farr_len} ]; do
@@ -2492,6 +2499,7 @@
                 eval __farr_get_value=\"\$\{${__farr_valname}_${__farr_idx}\}\"
                 _farr_acquire_object "${__farr_get_value}"
                 eval "${__farr_varname}"=\"\$\{__farr_get_value\}\"
+                [ -n "${__farr_idx_varname}" ] && eval "${__farr_idx_varname}"=${__farr_idx}
                 return 0
             fi
         else
--- a/tests/farray-alist.t	Tue Oct 08 17:55:23 2024 +0200
+++ b/tests/farray-alist.t	Tue Oct 08 18:11:24 2024 +0200
@@ -122,6 +122,19 @@
   V1
   $ falist_tryget _i LIST K
   [1]
+
+  $ falist_get _var LIST K2 _idx
+  $ echo "$_var"
+  V2 2
+  $ echo "$_idx"
+  2
+  $ falist_tryget _var LIST K1 _idx
+  $ echo "$_var"
+  V1
+  $ echo "$_idx"
+  1
+  $ falist_tryget _i LIST K _idx
+  [1]
   $ _var="$(falist_print_length NON_EXISTING_LIST)"
   ERROR: object `NON_EXISTING_LIST' not created properly: token empty
   $ echo "${_var}"