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 (1 - 9)
michaelk
post Feb 26 2016, 07:53 PM
Post #2


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

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



Try this:
#!/bin/bash

for (( i=0; i<=8; i++ ))
do
for (( j=0; j<=8; j++ ))
do
eval temp_${i}[$j]=$j
done

eval echo "array"=$i \${temp_${i}[@]}
done
Go to the top of the page
 
+Quote Post
Valjean
post Feb 27 2016, 10:00 AM
Post #3


Whats this Lie-nix Thing?
*

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



QUOTE (michaelk @ Feb 26 2016, 07:53 PM) *
Try this:
#!/bin/bash

for (( i=0; i<=8; i++ ))
do
for (( j=0; j<=8; j++ ))
do
eval temp_${i}[$j]=$j
done

eval echo "array"=$i \${temp_${i}[@]}
done


Thank you! In retrospect, this was obvious, I cannot believe I did not see it. Sorry for the delay in saying that this works, I've been consumed in trying to resolve my *next* coding problem...cheers that I figure it out on my own
Go to the top of the page
 
+Quote Post
michaelk
post Feb 27 2016, 11:20 AM
Post #4


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

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


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
michaelk
post Feb 27 2016, 04:54 PM
Post #6


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

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



Not sure since you left off the initialization of temp_x arrays and some variables. I don't know if the following simulates your code exactly but I can not duplicate your error.

CODE
for (( i=0; i<=8; i++ ))
do
  for (( j=0; j<=8; j++ ))
  do
     eval temp_${i}[$j]=$j
  done
  eval echo "array"=$i \${temp_${i}[@]}
done

for (( i=0; i<=8; i++ ))
do
  average[$i]=$i
done

for (( i=0; i<=8; i++ ))
do
  echo "Array=$i"
  for (( j=0; j<=8; j++ ))
  do
   var=${average[$j]}
   eval echo "before" \${temp_$i[@]}
   test=temp_$i[$j]
   eval temp_${i}[$j]=$( echo "scale=4; ${!test}-$var" | bc )
   eval echo "after  " \${temp_${i}[@]}
  done
done


Go to the top of the page
 
+Quote Post
Valjean
post Feb 27 2016, 05:49 PM
Post #7


Whats this Lie-nix Thing?
*

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



QUOTE (michaelk @ Feb 27 2016, 04:54 PM) *
Not sure since you left off the initialization of temp_x arrays and some variables. I don't know if the following simulates your code exactly but I can not duplicate your error.

CODE
for (( i=0; i<=8; i++ ))
do
  for (( j=0; j<=8; j++ ))
  do
     eval temp_${i}[$j]=$j
  done
  eval echo "array"=$i \${temp_${i}[@]}
done

for (( i=0; i<=8; i++ ))
do
  average[$i]=$i
done

for (( i=0; i<=8; i++ ))
do
  echo "Array=$i"
  for (( j=0; j<=8; j++ ))
  do
   var=${average[$j]}
   eval echo "before" \${temp_$i[@]}
   test=temp_$i[$j]
   eval temp_${i}[$j]=$( echo "scale=4; ${!test}-$var" | bc )
   eval echo "after  " \${temp_${i}[@]}
  done
done


I find the following interesting and confusing. I started playing with your code above, trying to track some differences. I imagine that the point of your second block was just to make some vector called "average," since you needed one to test what was going on. Is this the case? When I execute your code, I have no syntax errors. When I comment our your second block, where you define the average vector, and use my own from earlier in the code, I get an error. My average vector values are defined below; they are the result of some bc operations where I asked for four decimals. I would think that this implies that I have a problem with floating point vs. integer. My average vector before has values as evident from the following output:

array=1 0 1 2 3 4 5 6 7
array=2 0 1 2 3 4 5 6 7
My average vector before
.0200 -.1520 -.5180 .0820 .2560 .2240 .3460 .1240
His average vector after
0 1 2 3 4 5 6 7
Residual vector 1
0 0 0 0 0 0 0 0

and when I comment out your definition, I get the below output (note the decrease in the dimensionality of the vector by two):

array=1 0 1 2 3 4 5 6 7
array=2 0 1 2 3 4 5 6 7
My average vector before
.0200 -.1520 -.5180 .0820 .2560 .2240 .3460 .1240
His average vector after
.0200 -.1520 -.5180 .0820 .2560 .2240 .3460 .1240
(standard_in) 1: syntax error
(standard_in) 1: syntax error
(standard_in) 1: syntax error
(standard_in) 1: syntax error
Residual vector 1
-.0200 2.9180 3.7440 4.7760 5.6540 6.8760

CODE:

for (( i=1; i<=2; i++ ))
do
for (( j=0; j<8; j++ ))
do
eval temp_${i}[$j]=$j
done
eval echo "array"=$i \${temp_${i}[@]}
done

echo "My average vector before"
echo ${average[@]}

for (( i=0; i<8; i++ ))
do
average[$i]=$i
done

echo "His average vector after"
echo ${average[@]}

for (( i=1; i<=2; i++ ))
do
# echo "Array=$i"
for (( j=0; j<8; j++ ))
do
var=${average[$j]}
# eval echo "before" \${temp_$i[@]}
test=temp_$i[$j]
eval temp1_${i}[$j]=$( echo "scale=4; ${!test}-$var" | bc )
# eval echo "after " \${temp_${i}[@]}
done
done
echo "Residual vector 1"
echo ${temp1_1[@]}

Would it make sense that this is a floating point vs. integer problem?
Go to the top of the page
 
+Quote Post
michaelk
post Feb 27 2016, 08:11 PM
Post #8


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

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



Must be.
Go to the top of the page
 
+Quote Post
Valjean
post Feb 27 2016, 09:44 PM
Post #9


Whats this Lie-nix Thing?
*

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



QUOTE (michaelk @ Feb 27 2016, 08:11 PM) *
Must be.


I figured it out. It was simple enough. Subtraction must be written

$( echo "scale=4; ${!test} - ${var}" | bc )

with spaces before and after the operator (-); my original did not have spaces in between. I am mystified as to how the operation is such that it does not have a problem with

$( echo "scale=4; ${!test}-${var}" | bc )

for the first and last matrix subtractions, but does do screwy things on the middle matrix elements (and somehow decreases the dimensionality of the vector).

Thank you for your help.
Go to the top of the page
 
+Quote Post
michaelk
post Feb 28 2016, 06:24 AM
Post #10


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

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



Yep, that is it...
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: 20th October 2017 - 04:44 PM