bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Using variables in variables names


From: Dirk H. Schulz
Subject: Re: Using variables in variables names
Date: Tue, 14 Mar 2006 16:49:34 +0100
User-agent: Mozilla Thunderbird 1.0.7 (Macintosh/20050923)

Oh Mike,

thanks a lot for such detailled and well structured clarification! That did it, now I can use it (and it gave my script a speedup of nearly 70 % less running time).

Many thanks!

Dirk

Mike Stroyan schrieb:

On 3/13/06, Paul Jarc <prj@po.cwru.edu> wrote:
"Dirk H. Schulz" <dirk.schulz@kinzesberg.de> wrote:
Paul Jarc schrieb:
ac=12 eval "dings$ac=wasannersder"
And how do I reference it then?
ac=12 eval "value=\$dings$ac"
echo $value

Or:

ac=12 name=dings$ac echo ${!name}

It seems that you need to use the eval form instead of the ${!var} form
to handle array variables.  Here are some examples I played with.  The
pattern is to use a backslash to quote the $ for the array name.  The $i
in the array examples could be done as \$i because it works out the same
if it is expanded in either the first pass or the second pass.

$ suffix=one
$ eval "pre_${suffix}=simple1"
$ suffix=two
$ eval "pre_${suffix}=simple2"
$ suffix=one
$ eval "echo \$pre_${suffix}"
simple1
$ suffix=two
$ eval "echo \$pre_${suffix}"
simple2
$ suffix=one
$ i=1
$ eval "pre_A_${suffix}[$i]=array1_1"
$ i=2
$ eval "pre_A_${suffix}[$i]=array1_2"
$ suffix=two
$ i=1
$ eval "pre_A_${suffix}[$i]=array2_1"
$ i=3
$ eval "pre_A_${suffix}[$i]=array2_3"
$ set | grep pre_
_='pre_A_two[3]=array2_3'
pre_A_one=([1]="array1_1" [2]="array1_1")
pre_A_two=([1]="array2_1" [3]="array2_3")
pre_one=simple1
pre_two=simple2
$ i=1
$ eval "echo \${pre_A_${suffix}[$i]}"
array2_1
$ eval "echo \${pre_A_${suffix}[$i]}"
array2_1
$ i=3
$ eval "echo \${pre_A_${suffix}[$i]}"
array2_3
$ i=2
$ suffix=one
$ eval "echo \${pre_A_${suffix}[$i]}"
array1_2

--
Mike Stroyan
stroyan@gmail.com


_______________________________________________
Bug-bash mailing list
Bug-bash@gnu.org
http://lists.gnu.org/mailman/listinfo/bug-bash





reply via email to

[Prev in Thread] Current Thread [Next in Thread]