bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Aw: currently doable? Indirect notation used w/a hash


From: John Kearney
Subject: Aw: currently doable? Indirect notation used w/a hash
Date: Sat, 15 Jun 2013 15:41:31 +0200 (CEST)

   Sorry forgot the bit to retrive values
   It is possible to retrive numeric values without eval
   i.e.
   val=$((${ArrayName}[Index]))
   works quiet well and is quick, in fact I used to use this quiet a lot.


   There is also a backdoor approach that I don't really advise.
   val="${ArrayName}[Index]"
   echo "${!val}"
   What I actually tend to do is.
     ks_array_ChkName() {
       local LC_COLLATE=C
       case "${1:?Missing Variable Name}" in
         [!a-zA-Z_]* | *[!][a-zA-Z_0-9]* ) return 3;;
       esac
     }
     ks_val_Get() {
       ks_array_ChkName "${1:?Missing Destination Variable Name}" ||
   return $?
       ks_array_ChkName "${2:?Missing Source Variable Name}" || return $?
       eval "${1}=\"\${${2}:-}\""
     }
     ks_array_GetVal() { ks_val_Get "${1}" "${2}[${3}]" ; }
     ks_array_SetVal() { ks_val_Set "${1}[${2}]" "${3:-}" ; }

   Cheers

   Gesendet: Samstag, 15. Juni 2013 um 15:03 Uhr
   Von: "John Kearney" <dethrophes@web.de>
   An: "Linda Walsh" <bash@tlinx.org>
   Cc: bug-bash <bug-bash@gnu.org>
   Betreff: Aw: currently doable? Indirect notation used w/a hash
   In bash there are 2 options that I use.
   1.
   ArrayName=blah
   printf -V "${ArrayName}[Index]" "%s" "Value To Set"
   2.
   ks_val_ChkName() {
   local LC_COLLATE=C
   case "${1:?Missing Variable Name}" in
   [!a-zA-Z_]* | *[!a-zA-Z_0-9]* | '' ) return 3;;
   esac
   }
   ks_array_SetVal() {
   ks_val_ChkName "${1:?Missing Array Name}" || return $?
   ks_val_ChkName "a${2:?Missing Array Index}" || return $?
   eval "${1}"["${2}"]'="${3:-}"'
   }
   ks_array_SetVal "${ArrayName}" "Index" "Value"
   Cheers
   Gesendet: Sonntag, 09. Juni 2013 um 23:02 Uhr
   Von: "Linda Walsh" <bash@tlinx.org>
   An: bug-bash <bug-bash@gnu.org>
   Betreff: currently doable? Indirect notation used w/a hash
   I was wondering if I was missing some syntax somewhere...
   but I wanted to be able to pass the name of a hash in
   and store stuff in it and later retrieve it... but it
   looks like it's only possible with an eval or such?
   Would be nice....(??)*sigh*


reply via email to

[Prev in Thread] Current Thread [Next in Thread]