[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: No expansions performed while declaring an associative array using a
From: |
Chet Ramey |
Subject: |
Re: No expansions performed while declaring an associative array using a list of keys and values |
Date: |
Fri, 11 Dec 2020 11:46:01 -0500 |
User-agent: |
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0) Gecko/20100101 Thunderbird/78.5.1 |
On 12/11/20 9:45 AM, Léa Gris wrote:
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:
This is an interesting idea for a future bash version.
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
But I'm not really interested in writing a JSON parser and integrating it
into mapfile. Maybe someone would be interested in writing a loadable
builtin, along the lines of the `csv' loadable builtin in bash-5.1.
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU chet@case.edu http://tiswww.cwru.edu/~chet/
Re: No expansions performed while declaring an associative array using a list of keys and values, felix, 2020/12/14