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, 04:54 PM
Post #2


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

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


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

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: 23rd November 2017 - 06:11 AM