bash array names with variables

Robert Mckennon <[email protected]> Sat, 20 Oct 2012 23:37:18 -0400
Newsgroups gmane.org.user-groups.jaxlug
Message-ID <CAErk9YBxmT73=xTANOZnxqVui1m7AcVheT86VP3hHfaidnEiOQ@mail.gmail.com>
Is it possible to use a variables in an array name?

I'm having a hell of a time getting it to work.

for instance:   a_0[x], a_1[x]...etc  defined as: a_$i[x]

I use a loop to create the arrays a_$i
and then try and access them with:

arrayname=a_$i
nextarray=a_$(( $i + 1 ))


I found I can get the data out of an array with a variable-name like
so:   F[$i]=$(eval echo \${$arrayname[$N]})

and that works... (found that tip here
http://www.linuxquestions.org/questions/programming-9/bash-loop-over-variable-array-names-705702/
)


But when trying to put data into the variable-named arrays is where
I'm running into issues.

    52      if [ $j != $N ];
    53       then
    54        {
    55        $nextarray[$k]=$(eval echo \${$arrayname[$j]})
    56        k=$(( $k + 1 ))
    57        }
    58      fi

   ./random.sh: line 55: a_4[0]=: command not found

I've tried escaping it every way I can think of and google for, but
I'm about done banging my head against the wall...


Any suggestions???

Rob.

---------------------------------------------------------------------
Archive      http://marc.info/?l=jaxlug-list&r=1&w=2
RSS Feed     http://www.mail-archive.com/[email protected]/maillist.xml
Unsubscribe  [email protected]