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
> How to Zero a Loop of Declared Arrays
Valjean
post Feb 26 2016, 05:00 PM
Post #1


Whats this Lie-nix Thing?
*

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



The challenge I am facing is how to make a do loop which declares a bunch of arrays and then zeros them. I am struggling with this for the same reasons as described in a previous post (http://www.linuxhelp.net/forums/index.php?showtopic=11016); however, I believe the solution for that particular case is different from this particular case.

In pseudo-code, I want to declare a bunch of arrays

do i, max_i
do j, max_j
declare temp_$i[$j]
temp_$i[$j]=0
end do
end do

I can declare the arrays in a loop, but how can I zero them? In this case, I can cheat in that I know itís always a 1x8 array, but I still do not know how to zero it.

while [ $j -lt $helixmax ]
do
while [ $i -le $directory ]
do
declare -a temp1_$i
temp1_$i=(0 0 0 0 0 0 0 0 )
#temp_$i[$j]=0
#temp_$i[$j]=$test-${average[$j]}
i=$((i+1))
done
i=1
j=$((j+1))
done
Go to the top of the page
 
+Quote Post
 
Start new topic
Replies
michaelk
post Feb 27 2016, 11:20 AM
Post #2


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

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



No problem.
Go to the top of the page
 
+Quote Post
Valjean
post Feb 27 2016, 12:42 PM
Post #3


Whats this Lie-nix Thing?
*

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



QUOTE (michaelk @ Feb 27 2016, 11:20 AM) *
No problem.


Sadly, my ability to extrapolate from the above example and previous has hit a wall. Using the form for the generated arrays, very nicely zeroed, I have tried use them to do a basic matrix subtraction:

temp1_$i[$j] = temp_$i[$j] - average[j]

where each of the above is a 1x8 matrix. For simplicity of debugging, I reduced i to run from 1 to 2. Using the strategy of redirecting matrix elements into temporary variables from the previous posts, I tried to conduct this loop as being

matrix(i,j)= variable 1 - variable 2
where temp_$i[$j]=variable 1 and average[j] is variable 2.

The problem I am finding is that, somehow, in my loop, my line where the subtraction occurs somehow REDUCES the dimensionality of the 1x8 matrix by one. I have tracked down the problem to be line

eval temp1_${i}[$j]=$( echo "scale=4; ${!test}-$var1" | bc )"

I left a few of my print statements, before and after, showing that this line is the cause of the problem. It goes from a 1x8 matrix, through all of the j index, to a 1x7, after an error of

(standard_in) 1: syntax error

This happens one more time as it runs through the do loops, resulting in a 1x6. Then the loop repeats over i, and this happens to all of the vectors in the i loop.

Is it clear what is wrong with the syntax of the above statement to define the matrix elements of temp1_${i}?

CODE:

for (( i=0; i<=$directory; i++ ))
do
for (( j=0; j<$helixmax; j++ ))
do
eval temp1_${i}[$j]=0
done
done
#
#
#
echo ""
#for (( i=1; i<=$directory; i++ ))
for (( i=1; i<=2; i++ ))
do
for (( j=0; j<=8; j++ ))
do
test=temp_$i[$j]
var1=${average[$j]}
echo "Vector 1 is, before the operation"
echo "${temp1_1[@]}"
eval temp1_${i}[$j]=$( echo "scale=4; ${!test}-$var1" | bc )
echo "Vector 1 is, after the operation"
echo "${temp1_1[@]}"
done
done



And let's just say that I've learned my lesson that, after this code, I am learning Python and just working in that from now on...I've debated stopping and restarting in Python, but this work has a somewhat urgent need for finishing.
Go to the top of the page
 
+Quote Post

Posts in this topic
- Valjean   How to Zero a Loop of Declared Arrays   Feb 26 2016, 05:00 PM
- - michaelk   Try this: #!/bin/bash for (( i=0; i<=8; i+...   Feb 26 2016, 07:53 PM
|- - Valjean   QUOTE (michaelk @ Feb 26 2016, 07:53 PM) ...   Feb 27 2016, 10:00 AM
- - michaelk   No problem.   Feb 27 2016, 11:20 AM
|- - Valjean   QUOTE (michaelk @ Feb 27 2016, 11:20 AM) ...   Feb 27 2016, 12:42 PM
- - michaelk   Not sure since you left off the initialization of ...   Feb 27 2016, 04:54 PM
|- - Valjean   QUOTE (michaelk @ Feb 27 2016, 04:54 PM) ...   Feb 27 2016, 05:49 PM
- - michaelk   Must be.   Feb 27 2016, 08:11 PM
|- - Valjean   QUOTE (michaelk @ Feb 27 2016, 08:11 PM) ...   Feb 27 2016, 09:44 PM
- - michaelk   Yep, that is it...   Feb 28 2016, 06:24 AM


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: 16th December 2017 - 07:38 PM