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 (1 - 8)
michaelk
post Feb 24 2016, 07:28 PM
Post #2


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

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



echo "scale=4; ${temp_"$i"[j]} + ${sum[j]}" | bc

Typically in quoted strings you would need an escape character for quotes.

I would try without i.e.

echo "scale=4; ${temp_$i[j]} + ${sum[j]}" | bc

or if that does not work

echo "scale=4; ${temp_\"$i\"[j]} + ${sum[j]}" | bc


Go to the top of the page
 
+Quote Post
Valjean
post Feb 24 2016, 07:46 PM
Post #3


Whats this Lie-nix Thing?
*

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



QUOTE (michaelk @ Feb 24 2016, 07:28 PM) *
echo "scale=4; ${temp_"$i"[j]} + ${sum[j]}" | bc

Typically in quoted strings you would need an escape character for quotes.

I would try without i.e.

echo "scale=4; ${temp_$i[j]} + ${sum[j]}" | bc

or if that does not work

echo "scale=4; ${temp_\"$i\"[j]} + ${sum[j]}" | bc


No success on either proposal. What do the \ characters do as escape characters? Maybe if I better understand I can find a way to do this. I have been google searching about this, and it seems like the subject of indirect redirection is related to this?
Go to the top of the page
 
+Quote Post
michaelk
post Feb 24 2016, 09:44 PM
Post #4


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

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



There might be better ways to use a dynamic variable but here is one way.
#!/bin/bash
temp_2[0]=1
i=2
j=0
test=temp_$i[$j]
echo ${!test}
Go to the top of the page
 
+Quote Post
Valjean
post Feb 25 2016, 06:25 AM
Post #5


Whats this Lie-nix Thing?
*

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



OK! Making progress! Your suggestion worked in getting around the first hurdle, which was the big one! I thank you very much! My, how complicated, though. From what I have been reading, it seems that this problem is a general one in Linux formatting (but, again, I am a neophyte, what do I know). It has been suggested to me that I should learn python for these sorts of manipulations, later.

When I use the following code, and print out the sum array, my values seem good. However, I now have a different problem from the original: there is something wrong with my syntax in summing in old values in the array. The commented line is what I "want" to do, meaning to have a construct like

i=i+j

that is, the new value is equal to the old value plus a variable. However, when I attempt to execute the code as is, I get the error

(standard_in) 2: syntax error

which implies that I do not have the right syntax to read in the previous matrix element.

Code:

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

Is it obvious what I am doing wrong in trying to say array element=array element+variable? I am wondering if this is a case where the programming way of doing math (i.e., i=i+1 is nonsense mathematically) is a problem for an array?
Go to the top of the page
 
+Quote Post
michaelk
post Feb 25 2016, 08:06 AM
Post #6


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

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



bash syntax can be difficult at times.

I find nothing wrong with your syntax. It works for me on a single case.
sum[$j]=$( echo "scale=4; ${!test} + ${sum[$j]}" | bc )
Go to the top of the page
 
+Quote Post
Valjean
post Feb 25 2016, 12:13 PM
Post #7


Whats this Lie-nix Thing?
*

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



QUOTE (michaelk @ Feb 25 2016, 08:06 AM) *
bash syntax can be difficult at times.

I find nothing wrong with your syntax. It works for me on a single case.
sum[$j]=$( echo "scale=4; ${!test} + ${sum[$j]}" | bc )


How curious! I realize we are now in a no man's land of sorts because you cannot reproduce my error. The only thing I can think to ask is if the words (standard_in) 2: has any meaning to you that it can help me identify the mistake? I am unsure what to say, given the same line of code produces a problem for me. My total code is as follows, if it's any more insightful?

while [ $j -lt 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=1
j=$((j+1))
done
echo "${sum[@]}"

I appreciate your help in this.
Go to the top of the page
 
+Quote Post
michaelk
post Feb 25 2016, 03:44 PM
Post #8


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

Group: Support Specialist
Posts: 1,796
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 #9


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

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: 17th October 2017 - 01:52 PM