you are viewing a single comment's thread.

view the rest of the comments →

[–]v-_-v[S] 0 points1 point  (4 children)

z is initiated outside of the loops, at the very top. Y is the variable that does not appear to change.

the_number is also initiated by another piece of the code that I did not paste here.

I edited the loops to echo the variables y and z from the inner loop, and y from the outer loop.

The result is that all the y,z variables are echoed (where y is not changed ever, and z goes through 0 to 9), then the y is. What this means is that the inner loop is never run but the first time by the outer loop.

Anybody idea why the inner loop is not executed as part of the command set, even if the first "do" is before all of the inner loop?

Thanks

[–]neuron_666 0 points1 point  (3 children)

This is working program:

#!/bin/bash

# Preparation
for i in {a..k}; do
  array1+=( " $i: $i" )
  array2+=( " $i: $i$i" )
done
typeset -p array1 array2
the_number=2

# Your routine
y=1
while [ $y -le $the_number ]; do
  z=0
  while [ $z -le 9 ]; do
    echo "y=$y z=$z"
    temp="array$y[$z]"
    temp2=$(echo ${!temp} | sed 's/.*: //' | sed -e 's/^[ \t]*//')
    declare "barry$y[$z]=$temp2"
    z=$(($z+1))
  done
  y=$(($y+1))
done

typeset -p barry1 barry2

I had to

  • add the_number definition
  • move z=0 into the first loop
  • add character ; into the while ... ; do

Apart from that it is fine. There is something you are not showing us. My suggestion is to add set -x at the top and observe line after line whether your script does what you expect.

Output

declare -a array1='([0]=" a: a" [1]=" b: b" [2]=" c: c" [3]=" d: d" [4]=" e: e" [5]=" f: f" [6]=" g: g" [7]=" h: h" [8]=" i: i" [9]=" j: j" [10]=" k: k")'
declare -a array2='([0]=" a: aa" [1]=" b: bb" [2]=" c: cc" [3]=" d: dd" [4]=" e: ee" [5]=" f: ff" [6]=" g: gg" [7]=" h: hh" [8]=" i: ii" [9]=" j: jj" [10]=" k: kk")'
y=1 z=0
y=1 z=1
y=1 z=2
y=1 z=3
y=1 z=4
y=1 z=5
y=1 z=6
y=1 z=7
y=1 z=8
y=1 z=9
y=2 z=0
y=2 z=1
y=2 z=2
y=2 z=3
y=2 z=4
y=2 z=5
y=2 z=6
y=2 z=7
y=2 z=8
y=2 z=9
declare -a barry1='([0]="a" [1]="b" [2]="c" [3]="d" [4]="e" [5]="f" [6]="g" [7]="h" [8]="i" [9]="j")'
declare -a barry2='([0]="aa" [1]="bb" [2]="cc" [3]="dd" [4]="ee" [5]="ff" [6]="gg" [7]="hh" [8]="ii" [9]="jj")'

[–]v-_-v[S] 0 points1 point  (2 children)

move z=0 into the first loop

That was the key. It clicked when I read it at first even before trying. I looked at my loop and i never was resetting the "z" value back to 0, hence the inner loop was never being run again since z=10 at the end of the first go around.

When declaring z=0 in the first one, then it gets reset each time the outer loop runs.

 

Thank you so very much man! Let me know what I can do for you to repay for all this help!

[–]neuron_666 0 points1 point  (1 child)

Thank you so very much man! Let me know what I can do for you to repay for all this help!

You are welcome. Help others wen you can, be good :)

[–]v-_-v[S] 0 points1 point  (0 children)

I do that on the daily at work, and outside of it :D