Linux Help
guides forums blogs
Home Desktops Distributions ISO Images Logos Newbies Reviews Software Support & Resources Linuxhelp Wiki

Welcome Guest ( Log In | Register )



Advanced DNS Management
New ZoneEdit. New Managment.

FREE DNS Is Back

Sign Up Now
> Nested Variable Names in Array Summation
Valjean
post Feb 24 2016, 05:33 PM
Post #1


Whats this Lie-nix Thing?
*

Group: Members
Posts: 15
Joined: 22-February 16
Member No.: 19,047



I am struggling to write two nested do loops; there is a problem with repeat dollar sign declaration syntax. This is in bash.

Early in my program, I define

readarray -t temp_"$i" < frequency_values

in a loop over i, thus creating a whole bunch of arrays that are read in. This is fine. Each array is a 1x8 matrix.

Later on, I want to sum all of the matrix values of all the temporary arrays. I wrote

i=1
j=0
while [ $j -lt 8 ]
do
while [ $i -le $max ]
do
sum[$j]=$( echo "scale=4; ${temp_"$i"[$j]} + ${sum[$j]}" | bc )
i=$((i+1))
done
i=1
j=$((j+1))
done

The problem occurs in the sum line; bash objects to the quotes around $i in the name of the temp_$i arrays. I get the error:

line 35: scale=4; ${temp_"$i"[j]} + ${sum[j]}: bad substitution

How can I do a summation of matrix elements with a variable name of the array?

I have not googled this because I am honestly not even sure where to begin; I could not find a search string that was productive.

This post has been edited by Valjean: Feb 24 2016, 08:24 PM
Go to the top of the page
 
+Quote Post
 
Start new topic
Replies
michaelk
post Feb 25 2016, 03:44 PM
Post #2


Its GNU/Linuxhelp.net
*******

Group: Support Specialist
Posts: 1,800
Joined: 23-January 03
Member No.: 360



My guess is that you do not an initial value assigned to each variable.

#/bin/bash
i=0
j=0
max=8
sum[0]=0

for (( c=0; c<=8; c++ ))
do
sum[$c]=0
temp_0[$c]=$c
temp_1[$c]=$c
temp_2[$c]=$c
temp_3[$c]=$c
temp_4[$c]=$c
temp_5[$c]=$c
temp_6[$c]=$c
temp_7[$c]=$c
temp_8[$c]=$c
temp_9[$c]=$c
done

while [ $j -le 8 ]
do
while [ $i -le $max ]
do
temp_0[0]=0
test=temp_$i[$j]
#echo ${!test}
sum[$j]=$( echo "scale=4; ${!test} + ${sum[$j]}" | bc )
#sum[$j]=$( echo "scale=4; ${!test} + 0" | bc )
#echo "${sum[@]}"
i=$((i+1))
done
i=0
j=$((j+1))
done
echo "${sum[@]}"

The output
0 9 18 27 36 45 54 63 72
Go to the top of the page
 
+Quote Post
Valjean
post Feb 26 2016, 04:18 PM
Post #3


Whats this Lie-nix Thing?
*

Group: Members
Posts: 15
Joined: 22-February 16
Member No.: 19,047



QUOTE (michaelk @ Feb 25 2016, 03:44 PM) *
My guess is that you do not an initial value assigned to each variable.

#/bin/bash
i=0
j=0
max=8
sum[0]=0

for (( c=0; c<=8; c++ ))
do
sum[$c]=0
temp_0[$c]=$c
temp_1[$c]=$c
temp_2[$c]=$c
temp_3[$c]=$c
temp_4[$c]=$c
temp_5[$c]=$c
temp_6[$c]=$c
temp_7[$c]=$c
temp_8[$c]=$c
temp_9[$c]=$c
done

while [ $j -le 8 ]
do
while [ $i -le $max ]
do
temp_0[0]=0
test=temp_$i[$j]
#echo ${!test}
sum[$j]=$( echo "scale=4; ${!test} + ${sum[$j]}" | bc )
#sum[$j]=$( echo "scale=4; ${!test} + 0" | bc )
#echo "${sum[@]}"
i=$((i+1))
done
i=0
j=$((j+1))
done
echo "${sum[@]}"

The output
0 9 18 27 36 45 54 63 72


You are correct! Thank you!
Go to the top of the page
 
+Quote Post

Posts in this topic


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



RSS Lo-Fi Version Time is now: 18th November 2017 - 09:54 PM