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

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: 21st October 2017 - 11:47 PM