[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: associative array assignment from the output of a function
From: |
Chet Ramey |
Subject: |
Re: associative array assignment from the output of a function |
Date: |
Fri, 22 Oct 2010 08:56:45 -0400 |
> An easy example is better than an explanation:
>
> # Woks well
> indexarray()
> {
> echo "5 6 7"
> }
> declare -a t1=( $(indexarray) )
> echo ${t1[1]} ${t1[0]}
So you now have an indexed array named `t1'.
> # Does not work
> assocarray()
> {
> echo "[a]=5 [b]=6"
> }
> declare -A t1=( $(assocarray) ) # The error occurs here
And you're trying to redeclare it as an associative array here. The
variable already exists; you can't convert it between array types;
and bash tells you this. If you want an associative array, unset the
variable before you declare it.
If you really want this to work, you have to understand the order in
which assignments and expansions take place. You'll probably end up
using `eval', which is something you need to think about very carefully.
Chet
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRU chet@case.edu http://cnswww.cns.cwru.edu/~chet/