Le 11/12/2020 à 14:28, Oğuz écrivait :
Nah, this doesn't work either. Would be really useful if it did though.
$ declare -a foo=(1 2 )
$ declare -A assoc=("${foo[@]}" 3)
$ declare -p assoc
declare -A assoc=(["\"\${foo[@]}\""]="3" )
What would have been so useful is expanding mapfile to associative arrays:
key${IFS}value
declare -A assoc
IFS='= ' mapfile -t assoc <<INFILE
key1 = value1
key2=value2 with spaces
INFILE
And some dream of mapping JSON object members to associative arrays:
mapfile -j assoc <<JSON
{
"key1": "value1",
"key2": 42,
"key3": {
"otherkey": "othervalue"
}
}
JSON
translated to:
declare -A assoc(["key1"]="value1" ["key2"]="value2" ["key3"]=$'{\n
"otherkey": "othervalue"\n}'
--
Léa Gris