Re: Using variables in variables names
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 <[EMAIL PROTECTED]> wrote: "Dirk H. Schulz" <[EMAIL PROTECTED]> 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 [EMAIL PROTECTED] ___ Bug-bash mailing list Bug-bash@gnu.org http://lists.gnu.org/mailman/listinfo/bug-bash ___ Bug-bash mailing list Bug-bash@gnu.org http://lists.gnu.org/mailman/listinfo/bug-bash
Re: Using variables in variables names
On 3/13/06, Paul Jarc <[EMAIL PROTECTED]> wrote: > "Dirk H. Schulz" <[EMAIL PROTECTED]> 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 [EMAIL PROTECTED] ___ Bug-bash mailing list Bug-bash@gnu.org http://lists.gnu.org/mailman/listinfo/bug-bash
Re: Using variables in variables names
"Dirk H. Schulz" <[EMAIL PROTECTED]> 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} paul ___ Bug-bash mailing list Bug-bash@gnu.org http://lists.gnu.org/mailman/listinfo/bug-bash
Re: Using variables in variables names
Paul Jarc schrieb: "Dirk H. Schulz" <[EMAIL PROTECTED]> wrote: ac=12 dings$ac=wasannersder -bash: dings12=wasannersder: command not found Variable names in assignments are not subject to expansion. So since "dings$ac", as-is, does not fit the syntax for variable names, it isn't treated as an assignment. This will work: ac=12 eval "dings$ac=wasannersder" And how do I reference it then? ac=12 eval "dings$ac=wasannersder" echo $dings12 wasannersder # that works echo $('$dings'$ac) # trying to substitute $ac before $dings... -bash: $dings12: command not found Is there any way to reference it without anticipating the result of indirect referencing? Dirk ___ Bug-bash mailing list Bug-bash@gnu.org http://lists.gnu.org/mailman/listinfo/bug-bash
Re: Using variables in variables names
"Dirk H. Schulz" <[EMAIL PROTECTED]> wrote: >> ac=12 dings$ac=wasannersder >> -bash: dings12=wasannersder: command not found Variable names in assignments are not subject to expansion. So since "dings$ac", as-is, does not fit the syntax for variable names, it isn't treated as an assignment. This will work: ac=12 eval "dings$ac=wasannersder" paul ___ Bug-bash mailing list Bug-bash@gnu.org http://lists.gnu.org/mailman/listinfo/bug-bash
Using variables in variables names
Hi folks, I am sure this has been asked quite some times, but I did not find anything inspiring or helpful - in fact not too much at all. For accelerating a script I need the possibility to set up an unknown number of arrays and to name them (at least partly) with values of a variable. It is like defining arrays with names that contain an increasing number: array$x. While poking around I found out: This does not work at all, even with simple variables it does not. dings=bums echo $dings bums ac=12 dings$ac=wasannersder -bash: dings12=wasannersder: command not found I looked very deeply into man bash and any manual and howto I found, but did not find out any reason why this does not work. It must be one of the basic principles of expansion and assignment, but I would like to understand it. And then the question of multi-dimensional arrays (see above). The "Advanced Bash Scripting Guide" mentions that it is possible to have them using indirect referencing - but I did not understand how this could be done. Any idea, hint or help? It would be a great relief after two days of search and research. Dirk ___ Bug-bash mailing list Bug-bash@gnu.org http://lists.gnu.org/mailman/listinfo/bug-bash