You'd want a mapping of "keys" to "values", where "values" are the lists of strings, and the "keys" are ARGENT
, BIENETRE
…
You're on the right path with aoarrs
, because you could use that array as associative array:
declare -A aoarrs aoarrs[ARGENT]=$ARGENT aoarrs[BIENETRE]=$BIENETRE
and then just iterate over all keys in that array using something like for key in ${!aoarrs[@]}…
.
Sadly, bash doesn't, for whatever reason, allow lists to be elements of these associative arrays.
So, things suck. You can for example join the elements of your lists with a reserved character to split them later on (that's stupid because it means you can't have all characters in your string, or need to start escaping them), or you build your own functions that take lists of strings, append them to a large array and then you implement your own associative lookup function on that container (that would be stupid; not only would it be slow, it would also require you to write relatively much code in a relatively ill-suited language). It would look terrible. Here's an example which I write down without testing it, because it's ugly enough that I need to get it out of my head, but don't want to deal with it any further:
#!/bin/bash ############################### # UNTESTED STUFF # # If you think this code is # # acceptable, consider doing # # something good for yourself # # once in a while # ############################### declare -A index declare -A lengths declare -a storage # Adds an entry to our our custom container # # Usage: # add_key_values KEY "list element 1" "list element 2" … function add_key_values() { local key="$1" shift local -n valuelist=$@ # get the length of the passed list, to save it local lengthlist=${#valuelist[@]} # get the end of the current storage, that's where we start adding # our list local start_index=${#storage[@]} # finally, actually store the list items in the storage for item in "${valuelist[@]}"; do storage+=("${item}") done lengths["${key}"]=$lengthlist index["${key}"]=$start_index } # Retrieve a list from the storage # Sadly, bash is not a proper programming language, because what it calls # "functions" don't do the one thing that a function needs to do: # return a value for an argument. There's just no "return" mechanism in bash. # # Returns an empty list if the key wasn't found. # # So, after sobbing a bit, we just say # Usage: # get_values $key # Overwrites the `ret_val` variable with the list that was saved earlier function get_values() { # prepare ret_val declare -g -a ret_val=() local key=$1 # We return (with ret_val empty) if key isn't present # frigging bash doesn't have a "is key present in associative array" function… # so this is the workaround to check whether there's $key in $index. # seriously. local teststring teststring="$(printf 'index[%s]' "${key}")" # you need quite modern bash to even get the "is defined" -v test [[ -v "${teststring}" ]] || return # let's get all the elements from storage and append them to ret_val local start=${index[$key]} local length=${lengths[$key]} for idx in $(seq $(( start - 1 )) $((start - 1 + length)) ); do ret_val+=("${storage[idx]}") done } #################### # EXAMPLE USAGE #################### add_key_values "ARGENT" "Nous devons économiser de l'argent." "Je dois économiser de l'argent." add_key_values "BIENETRE" ("Comment vas-tu?" "Tout va bien ?") for key in ${!index[@]}; do echo "the list for value $key contains" get_values "${key}" for element in ${ret_val[@]}; do echo "element: ${element}" done done
The next option is magic that involves "indirect addressing" of variables by name using eval
. That's kind of evil, and stupid, and there's very many posts on here that hint at "if you're at that point, then maybe use a proper programming language instead of bash".
I'd concur with that: This whole problem would literally be done in four lines of python, with the first two lines would be storing "ARGENT" and "BIENETRE" and their lists into a dict
. Or really, in any other common language that's not bash (or C, for that matter), associative arrays are less bad.
${ARGENT}
is the same as${ARGENT[0]}