Variable substitution with arrays


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Variable substitution with arrays
# 1  
Old 08-24-2017
Variable substitution with arrays

Hi all,

I have a script with the following gist:

Code:
declare -a index=(0 1 2 3 4);

declare -a animals=(dog cat horse penguin cow);

declare -a fruits=(orange apple grapes peach mango);

declare -a drinks=(juice milk coffee tea coke);

declare -a cities=(toronto paris london glasgow sydney);

declare -a countries=(canada france england scotland australia);

declare -a all=(animals fruits drinks cities countries);

for i in "${index[@]}" ; do
	echo ${i};
	animals="${animals["${i}"]}";
	echo $animals;
	fruits="${fruits["${i}"]}";
	echo $fruits;
	drinks="${drinks["${i}"]}";
	echo $drinks;
	cities="${cities["${i}"]}";
	echo $cities;
	countries="${countries["${i}"]}";
	echo $countries;
done

And this is the output:

Code:
0
dog
orange
juice
toronto
canada
1
cat
apple
milk
paris
france
2
horse
grapes
coffee
london
england
3
penguin
peach
tea
glasgow
scotland
4
cow
mango
coke
sydney
australia

Because my actual script has more arrays to loop through and I'll be adding more as time goes on, I came up with an 'all' array that has the array names in it. Then I attempted to loop through each array as such:

Code:
for i in "${index[@]}" ; do
	echo ${i};
	for j in "${all[@]}" ; do
		echo ${!j[${i}]};
	done
done

But this time I'm only getting items at index 0, as per the output below:

Code:
0
dog
orange
juice
toronto
canada
1





2





3





4





abc@xyz$


I've been struggling with this one for a while now, and I'm not entirely sure what I'm trying to accomplish is possible with Bash. Any help is appreciated!


Thanks!
# 2  
Old 08-25-2017
I don't know if you can do what you're trying to do with bash, but you can do it with a 1993 or later version of ksh by using a name reference variable. You can't do it quite the way you were trying to do it because you can't currently create arrays of nameref variables, but the following seems to produce results similar to what I think you were trying to do:
Code:
#!/bin/ksh
animals=(dog cat horse penguin cow)
fruits=(orange apple grapes peach mango)
drinks=(juice milk coffee tea coke)
cities=(toronto paris london glasgow sydney)
countries=(canada france england scotland australia)
sparse[1]=first
sparse[10]=tenth
sparse[33]='thirty-third'
sparse[100]=hundredth

typeset -A associative
associative["a b"]='A B'
associative["x y z"]='X Y Z'
associative["SomeOtherString"]='Anything you might want'

arrays=(animals fruits drinks cities countries sparse associative)
typeset -n arrayname

for arrayname in "${arrays[@]}"
do	for subscript in "${!arrayname[@]}"
	do	printf '%s[%s]=%s\n' "${!arrayname}" "$subscript" "${arrayname[$subscript]}"
	done
done

which produces the output:
Code:
animals[0]=dog
animals[1]=cat
animals[2]=horse
animals[3]=penguin
animals[4]=cow
fruits[0]=orange
fruits[1]=apple
fruits[2]=grapes
fruits[3]=peach
fruits[4]=mango
drinks[0]=juice
drinks[1]=milk
drinks[2]=coffee
drinks[3]=tea
drinks[4]=coke
cities[0]=toronto
cities[1]=paris
cities[2]=london
cities[3]=glasgow
cities[4]=sydney
countries[0]=canada
countries[1]=france
countries[2]=england
countries[3]=scotland
countries[4]=australia
sparse[1]=first
sparse[10]=tenth
sparse[33]=thirty-third
sparse[100]=hundredth
associative[SomeOtherString]=Anything you might want
associative[a b]=A B
associative[x y z]=X Y Z

These 2 Users Gave Thanks to Don Cragun For This Post:
# 3  
Old 08-25-2017
Quote:
Originally Posted by Kingzy
Then I attempted to loop through each array as such:

Code:
for i in "${index[@]}" ; do
	echo ${i};
	for j in "${all[@]}" ; do
		echo ${!j[${i}]};
	done
done

But this time I'm only getting items at index 0, as per the output below:
The underlying problem is: arrays in bash (and ksh88 as well) are ONE-dimensional. Therefore, you can create a variable holding a one-dimensional array, but you can't put other array variables as elements into this array.

You could use the following workaround, but i strongly suggest you don't. Stretching the limits of what can be done is fun and helps learning the trade, but you shouldn't put circus tricks into production code. So, with this (rather big) grain of salt, here it goes:

Variables are evaluated always in the same step and all at the same time, which is why you cannot do things like this:

Code:
xfoo="abc"
yfoo="def"
selector="x"

echo ${${selector}foo}

This would rely on "${selector}" to be evaluated first and only then the resulting "${xfoo}" to be evaluated again. But, as i said, this is not the case and therefore this will fail.

There is one remedy for that, though: the keyword eval. eval starts the evaluation process again and this way you get (among other things) a second evaluation phase for your variables:

Code:
xfoo="abc"
yfoo="def"
selector="x"

eval echo \${${selector}foo}

The same way you can create sort-of two-dimensional arrays by using this mechanism:

Code:
arr1[1]="arr1.1"
arr1[2]="arr1.2"
arr1[3]="arr1.3"
arr1[4]="arr1.4"

arr2[1]="arr2.1"
arr2[2]="arr2.2"
arr2[3]="arr2.3"
arr2[4]="arr2.4"

arr3[1]="arr3.1"
arr3[2]="arr3.2"
arr3[3]="arr3.3"
arr3[4]="arr3.4"


for i in 1 2 3 ; do
     for j in 1 2 3 4 ; do
         eval echo \${arr${i}[$j]}
     done
done

But again: avoid eval like the plague and if you have to use it this is usually indicative that you better search for an alternative. As a show-off of skill, though, it is pretty cool. No?

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 4  
Old 08-25-2017
Hi Don and Bakunin,

Thank you both for your replies and explanations.

Don: It looks like I could accomplish what I want with ksh, except the output would need to be grouped by index as opposed to array names:

Code:
animals[0]=dog
fruits[0]=orange
drinks[0]=juice
cities[0]=toronto
countries[0]=canada

animals[1]=cat
fruits[1]=apple
drinks[1]=milk
cities[1]=paris
countries[1]=france

animals[2]=horse
fruits[2]=grapes
drinks[2]=coffee
cities[2]=london
countries[2]=england

animals[3]=penguin
fruits[3]=peach
drinks[3]=tea
cities[3]=glasgow
countries[3]=scotland

animals[4]=cow
fruits[4]=mango
drinks[4]=coke
cities[4]=sydney
countries[4]=australia



Quote:
You can't do it quite the way you were trying to do it because you can't currently create arrays of nameref variables
Quote:
The underlying problem is: arrays in bash (and ksh88 as well) are ONE-dimensional. Therefore, you can create a variable holding a one-dimensional array, but you can't put other array variables as elements into this array.
I had a feeling I'd get that kind of answer but was still hoping for a syntax fix Smilie


Quote:
This would rely on "${selector}" to be evaluated first and only then the resulting "${xfoo}" to be evaluated again. But, as i said, this is not the case and therefore this will fail.
Yes that's exactly what I wanted, evaluate "${selector}" first then "${xfoo}" :S

Because my script is pretty much done and I've never written ksh before, I guess I'll just stick to adding array names manually as I go. At least this implementation works in bash.

Thanks!

---------- Post updated at 12:11 PM ---------- Previous update was at 12:09 PM ----------

Quote:
As a show-off of skill, though, it is pretty cool. No?
It's always good to know Smilie
# 5  
Old 08-25-2017
Don't despair - typeset -n is understood by bash as well:

Code:
typeset -n j
for i in "${index[@]}" ; do    echo ${i};      for j in "${all[@]}" ; do echo ${!j}, ${j[$i]};         done; done
0
animals, dog
fruits, orange
drinks, juice
cities, toronto
countries, canada
1
animals, cat
fruits, apple
drinks, milk
cities, paris
countries, france
2
animals, horse
fruits, grapes
drinks, coffee
cities, london
countries, england
3
animals, penguin
fruits, peach
drinks, tea
cities, glasgow
countries, scotland
4
animals, cow
fruits, mango
drinks, coke
cities, sydney
countries, australia

And, with eval, it would look like

Code:
for i in "${index[@]}" ; do    echo ${i};      for j in "${all[@]}" ; do eval echo \$j, \${$j[\$i]};   done; done
0
animals, dog
fruits, orange
drinks, juice
cities, toronto
countries, canada
1
animals, cat
fruits, apple
drinks, milk
cities, paris
countries, france
2
animals, horse
fruits, grapes
drinks, coffee
cities, london
countries, england
3
animals, penguin
fruits, peach
drinks, tea
cities, glasgow
countries, scotland
4
animals, cow
fruits, mango
drinks, coke
cities, sydney
countries, australia

Does any of these come close to what you want?
# 6  
Old 08-25-2017
Hello RudiC,

Yes that's what I want, with the correct grouping and all. Smilie

I am getting the expected output with 'eval' but not so with typeset:

Code:
abc@xyz$ ./loop.sh
./loop.sh: line 17: typeset: -n: invalid option
typeset: usage: typeset [-afFirtx] [-p] name[=value] ...
0
dog, animals
orange, fruits
juice, drinks
toronto, cities
canada, countries
1
dog,
orange,
juice,
toronto,
canada,
2
dog,
orange,
juice,
toronto,
canada,
3
dog,
orange,
juice,
toronto,
canada,
4
dog,
orange,
juice,
toronto,
canada,
abc@xyz$ bash --version
GNU bash, version 4.4.12(1)-release (x86_64-apple-darwin16.3.0)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

I'm using side-loaded bash 4 on OS X courtesy of Homebrew, if it matters.

Also, I'm confused as to why the order is flipped.

Code:
0
dog, animals

instead of

Code:
0
animals, dog

# 7  
Old 08-25-2017
Hi RudiC,
Thanks for letting me know bash now recognizes typeset -n. (It doesn't in the bash I have on macOS Sierra 10.12.6 which includes GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin16). And, apparently it doesn't with the bash on whatever OS Kingzy is using.)

Hi Kingzy,
It is always a good idea to tell us what operating system you're using (in addition to the shell). If we know what OS and shells you have available, we're less likely to offer suggestions that won't work in your environment. Assuming that you have access to a 1993 or later version of the Korn shell, the following should work...

I had hoped that with the example code I had provided, you would have been able to reconfigure the loops in my example to get whatever output was desired. For the output format requested in post #4, the following should suffice:
Code:
#!/bin/ksh
index=(0 1 2 3 4)
all=(animals fruits drinks cities countries)

animals=(dog cat horse penguin cow)
fruits=(orange apple grapes peach mango)
drinks=(juice milk coffee tea coke)
cities=(toronto paris london glasgow sydney)
countries=(canada france england scotland australia)

typeset -n arrayname

separator=
for subscript in "${!index[@]}"
do	printf "$separator"
	separator='\n'
	for arrayname in "${all[@]}"
	do	printf '%s[%s]=%s\n' "${!arrayname}" "$subscript" "${arrayname[$subscript]}"
	done
done

which just switches the inner and outer loops in the code I suggested before, changes the names of the arrays used as looping values, and adds code to add an empty line between groups. The above produces the output requested in post #4.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Variable substitution

Hi, I have to write a shell script in which I have to substitute a variable within a variable. For example, var1=aaa var2=file.$var1.txt The output should be, echo $var2 file.aaa.txt Can someone please help me in getting this. I tried using eval, but it didnt work. I might be using it... (2 Replies)
Discussion started by: grajp002
2 Replies

2. Shell Programming and Scripting

How to use variable with command substitution in variable

For example I have variable like below echo $OUTPUT /some/path/`uname -n` when I try to use the variable OUTPUT like below cd $OUTPUT or cd ${OUTPUT} I am getting bad substituion error message $ cd $OUTPUT ksh: cd: bad substitution $ cd ${OUTPUT} ksh: cd: bad substitution ... (1 Reply)
Discussion started by: rajukv
1 Replies

3. Shell Programming and Scripting

Variable Substitution

Hi , I have a variable as follows, Temp=`cat ABC.txt | cut -c5-` This will yeild a part of the date. say , 200912. I would like to substitute this variable's value in a filename. eg: File200912F.zip when i say File$TempF.zip , it is not substituting. Any help ? Thanks in... (2 Replies)
Discussion started by: mohanpadamata
2 Replies

4. Shell Programming and Scripting

bash: combine arrays with weird substitution/references

Hi all. I'm trying to finish a bash script with the following elements: ARRAY="blah $ITEM blah blah" ARRAY="blah blah $ITEM blah bluh" #ARRAY="...." # ...the ARRAY elements represent a variable but defined # syntax and they're all hard-coded in the script. #(...) ITEMS='1.0 2.3... (2 Replies)
Discussion started by: yomaya
2 Replies

5. Shell Programming and Scripting

variable substitution

file1.ksh #!/bin/ksh test5_create="I am a man" # test5 will be dynamic and the value will be passed from command line a=${1}_create echo $a # i need the output as "I am a man" ./file1.ksh test5 # i run the script like this any suggessions guys... (1 Reply)
Discussion started by: giri_luck
1 Replies

6. UNIX for Dummies Questions & Answers

Variable substitution

Hi, That might be pretty simple. How can I generate a variable name and get their value ? Thanks a lot. Something like: >CUSTOMER_NF=26 > object=CUSTOMER > echo ${object}_NF CUSTOMER_NF > echo ${${object}_NF} ksh: ${${object}_NF}: 0403-011 The specified substitution is... (7 Replies)
Discussion started by: Leo_NN
7 Replies

7. Shell Programming and Scripting

Sed variable substitution when variable constructed of a directory path

Hello, i have another sed question.. I'm trying to do variable substition with sed and i'm running into a problem. my var1 is a string constructed like this: filename1 filerev1 filepath1 my var2 is another string constructed like this: filename2 filerev2 filepath2 when i do... (2 Replies)
Discussion started by: alrinno
2 Replies

8. Shell Programming and Scripting

Variable Substitution

I have run into a wall with my iptables firewall scripting. I am blocking all of the private side IP addresses on the WAN interface on systems running NAT. However, if the system is not running NAT and needs to allow access to the local LAN on the WAN interface, I need to block all but one of... (2 Replies)
Discussion started by: garak
2 Replies

9. UNIX for Dummies Questions & Answers

variable substitution

Hi everyone, I have a simple question to ask : In a script that I'm writting, I need to create variables on-the-fly. For instance, for every iterartion of the following loop a var_X variable should be generated : #!/bin/ksh a="1 2 3" for i in $a do var_${i}=$i echo "${var_$i}" done ... (1 Reply)
Discussion started by: ck-18
1 Replies

10. UNIX for Advanced & Expert Users

Substitution in a variable

Hey All, I'm trying to clean up a variable using sed but It dosn't seem to work. I'm trying to find all the spaces and replace them with "\ " (a slash and a space). For Example "Hello World" should become "Hello\ World". But it does nothing. If I put it directly into the command line it works... (3 Replies)
Discussion started by: spragueg
3 Replies
Login or Register to Ask a Question