why does this simple script give such a weird output?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
I've just started learning how to use arrays so sorry if the answer is somehow obvious. I made a simple program which i thought would just count normally. The output of this program ended up being not only backwards at first but very strange. the program is:
#!/bin/bash
num=1
declare -A A1
while true
do
A1[$(($num + 1))]=$num
num=$(( $num + 1 ))
e=0
for e in ${A1[@]}
do
echo $e
done
echo "e is $e"
echo "num is $num"
sleep 0.1
done
but it's output was started doing some really weird stuff after 8 as shown here:
1
e is 1
num is 2
2
1
e is 1
num is 3
3
2
1
e is 1
num is 4
4
3
2
1
e is 1
num is 5
5
4
3
2
1
e is 1
num is 6
6
5
4
3
2
1
e is 1
num is 7
7
6
5
4
3
2
1
e is 1
num is 8
8
7
6
5
4
3
2
1
e is 1
num is 9
8
7
6
5
4
3
2
1
9
e is 9
num is 10
8
7
6
5
4
3
2
1
9
10
e is 10
num is 11
8
7
6
5
4
3
2
1
11
9
10
e is 10
num is 12
8
7
6
5
4
3
2
1
11
12
9
10
e is 10
num is 13
8
7
6
5
4
3
2
1
11
12
9
10
13
e is 13
num is 14
8
7
6
5
4
3
2
1
11
12
9
10
13
14
e is 14
num is 15
8
7
6
5
4
3
2
1
11
12
9
10
15
13
14
e is 14
num is 16
8
7
6
5
4
3
2
1
11
12
9
10
15
16
13
14
e is 14
num is 17
8
7
6
5
4
3
2
1
17
11
12
9
10
15
16
13
14
e is 14
num is 18
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
e is 14
num is 19
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
19
e is 19
num is 20
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
20
19
e is 19
num is 21
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
21
20
19
e is 19
num is 22
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
22
21
20
19
e is 19
num is 23
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
23
22
21
20
19
e is 19
num is 24
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
24
23
22
21
20
19
e is 19
num is 25
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
25
24
23
22
21
20
19
e is 19
num is 26
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
26
25
24
23
22
21
20
19
e is 19
num is 27
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
26
25
24
23
22
21
20
19
27
e is 27
num is 28
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
26
25
24
23
22
21
20
19
28
27
e is 27
num is 29
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
26
25
24
23
22
21
20
19
28
27
29
e is 29
num is 30
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
26
25
24
23
22
21
20
19
28
27
29
30
e is 30
num is 31
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
26
25
24
23
22
21
20
19
28
27
29
30
31
e is 31
num is 32
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
26
25
24
23
22
21
20
19
28
27
29
30
31
32
e is 32
num is 33
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
26
25
24
23
22
21
20
19
28
27
33
29
30
31
32
e is 32
num is 34
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
26
25
24
23
22
21
20
19
28
27
33
34
29
30
31
32
e is 32
num is 35
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
26
25
24
23
22
21
20
19
28
27
33
34
35
29
30
31
32
e is 32
num is 36
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
26
25
24
23
22
21
20
19
28
27
33
34
35
36
29
30
31
32
e is 32
num is 37
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
26
25
24
23
22
21
20
19
28
27
33
34
35
36
29
30
31
32
37
e is 37
num is 38
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
26
25
24
23
22
21
20
19
28
27
33
34
35
36
29
30
31
32
37
38
e is 38
num is 39
39
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
26
25
24
23
22
21
20
19
28
27
33
34
35
36
29
30
31
32
37
38
e is 38
num is 40
^C
can anybody explain this?
command-line bash scripts
add a comment |
I've just started learning how to use arrays so sorry if the answer is somehow obvious. I made a simple program which i thought would just count normally. The output of this program ended up being not only backwards at first but very strange. the program is:
#!/bin/bash
num=1
declare -A A1
while true
do
A1[$(($num + 1))]=$num
num=$(( $num + 1 ))
e=0
for e in ${A1[@]}
do
echo $e
done
echo "e is $e"
echo "num is $num"
sleep 0.1
done
but it's output was started doing some really weird stuff after 8 as shown here:
1
e is 1
num is 2
2
1
e is 1
num is 3
3
2
1
e is 1
num is 4
4
3
2
1
e is 1
num is 5
5
4
3
2
1
e is 1
num is 6
6
5
4
3
2
1
e is 1
num is 7
7
6
5
4
3
2
1
e is 1
num is 8
8
7
6
5
4
3
2
1
e is 1
num is 9
8
7
6
5
4
3
2
1
9
e is 9
num is 10
8
7
6
5
4
3
2
1
9
10
e is 10
num is 11
8
7
6
5
4
3
2
1
11
9
10
e is 10
num is 12
8
7
6
5
4
3
2
1
11
12
9
10
e is 10
num is 13
8
7
6
5
4
3
2
1
11
12
9
10
13
e is 13
num is 14
8
7
6
5
4
3
2
1
11
12
9
10
13
14
e is 14
num is 15
8
7
6
5
4
3
2
1
11
12
9
10
15
13
14
e is 14
num is 16
8
7
6
5
4
3
2
1
11
12
9
10
15
16
13
14
e is 14
num is 17
8
7
6
5
4
3
2
1
17
11
12
9
10
15
16
13
14
e is 14
num is 18
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
e is 14
num is 19
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
19
e is 19
num is 20
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
20
19
e is 19
num is 21
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
21
20
19
e is 19
num is 22
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
22
21
20
19
e is 19
num is 23
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
23
22
21
20
19
e is 19
num is 24
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
24
23
22
21
20
19
e is 19
num is 25
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
25
24
23
22
21
20
19
e is 19
num is 26
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
26
25
24
23
22
21
20
19
e is 19
num is 27
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
26
25
24
23
22
21
20
19
27
e is 27
num is 28
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
26
25
24
23
22
21
20
19
28
27
e is 27
num is 29
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
26
25
24
23
22
21
20
19
28
27
29
e is 29
num is 30
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
26
25
24
23
22
21
20
19
28
27
29
30
e is 30
num is 31
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
26
25
24
23
22
21
20
19
28
27
29
30
31
e is 31
num is 32
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
26
25
24
23
22
21
20
19
28
27
29
30
31
32
e is 32
num is 33
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
26
25
24
23
22
21
20
19
28
27
33
29
30
31
32
e is 32
num is 34
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
26
25
24
23
22
21
20
19
28
27
33
34
29
30
31
32
e is 32
num is 35
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
26
25
24
23
22
21
20
19
28
27
33
34
35
29
30
31
32
e is 32
num is 36
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
26
25
24
23
22
21
20
19
28
27
33
34
35
36
29
30
31
32
e is 32
num is 37
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
26
25
24
23
22
21
20
19
28
27
33
34
35
36
29
30
31
32
37
e is 37
num is 38
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
26
25
24
23
22
21
20
19
28
27
33
34
35
36
29
30
31
32
37
38
e is 38
num is 39
39
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
26
25
24
23
22
21
20
19
28
27
33
34
35
36
29
30
31
32
37
38
e is 38
num is 40
^C
can anybody explain this?
command-line bash scripts
2
You're iterating on associative array keys. Associative arrays aren't necessarily sorted.
– Sergiy Kolodyazhnyy
Mar 31 at 6:13
add a comment |
I've just started learning how to use arrays so sorry if the answer is somehow obvious. I made a simple program which i thought would just count normally. The output of this program ended up being not only backwards at first but very strange. the program is:
#!/bin/bash
num=1
declare -A A1
while true
do
A1[$(($num + 1))]=$num
num=$(( $num + 1 ))
e=0
for e in ${A1[@]}
do
echo $e
done
echo "e is $e"
echo "num is $num"
sleep 0.1
done
but it's output was started doing some really weird stuff after 8 as shown here:
1
e is 1
num is 2
2
1
e is 1
num is 3
3
2
1
e is 1
num is 4
4
3
2
1
e is 1
num is 5
5
4
3
2
1
e is 1
num is 6
6
5
4
3
2
1
e is 1
num is 7
7
6
5
4
3
2
1
e is 1
num is 8
8
7
6
5
4
3
2
1
e is 1
num is 9
8
7
6
5
4
3
2
1
9
e is 9
num is 10
8
7
6
5
4
3
2
1
9
10
e is 10
num is 11
8
7
6
5
4
3
2
1
11
9
10
e is 10
num is 12
8
7
6
5
4
3
2
1
11
12
9
10
e is 10
num is 13
8
7
6
5
4
3
2
1
11
12
9
10
13
e is 13
num is 14
8
7
6
5
4
3
2
1
11
12
9
10
13
14
e is 14
num is 15
8
7
6
5
4
3
2
1
11
12
9
10
15
13
14
e is 14
num is 16
8
7
6
5
4
3
2
1
11
12
9
10
15
16
13
14
e is 14
num is 17
8
7
6
5
4
3
2
1
17
11
12
9
10
15
16
13
14
e is 14
num is 18
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
e is 14
num is 19
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
19
e is 19
num is 20
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
20
19
e is 19
num is 21
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
21
20
19
e is 19
num is 22
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
22
21
20
19
e is 19
num is 23
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
23
22
21
20
19
e is 19
num is 24
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
24
23
22
21
20
19
e is 19
num is 25
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
25
24
23
22
21
20
19
e is 19
num is 26
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
26
25
24
23
22
21
20
19
e is 19
num is 27
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
26
25
24
23
22
21
20
19
27
e is 27
num is 28
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
26
25
24
23
22
21
20
19
28
27
e is 27
num is 29
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
26
25
24
23
22
21
20
19
28
27
29
e is 29
num is 30
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
26
25
24
23
22
21
20
19
28
27
29
30
e is 30
num is 31
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
26
25
24
23
22
21
20
19
28
27
29
30
31
e is 31
num is 32
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
26
25
24
23
22
21
20
19
28
27
29
30
31
32
e is 32
num is 33
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
26
25
24
23
22
21
20
19
28
27
33
29
30
31
32
e is 32
num is 34
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
26
25
24
23
22
21
20
19
28
27
33
34
29
30
31
32
e is 32
num is 35
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
26
25
24
23
22
21
20
19
28
27
33
34
35
29
30
31
32
e is 32
num is 36
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
26
25
24
23
22
21
20
19
28
27
33
34
35
36
29
30
31
32
e is 32
num is 37
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
26
25
24
23
22
21
20
19
28
27
33
34
35
36
29
30
31
32
37
e is 37
num is 38
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
26
25
24
23
22
21
20
19
28
27
33
34
35
36
29
30
31
32
37
38
e is 38
num is 39
39
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
26
25
24
23
22
21
20
19
28
27
33
34
35
36
29
30
31
32
37
38
e is 38
num is 40
^C
can anybody explain this?
command-line bash scripts
I've just started learning how to use arrays so sorry if the answer is somehow obvious. I made a simple program which i thought would just count normally. The output of this program ended up being not only backwards at first but very strange. the program is:
#!/bin/bash
num=1
declare -A A1
while true
do
A1[$(($num + 1))]=$num
num=$(( $num + 1 ))
e=0
for e in ${A1[@]}
do
echo $e
done
echo "e is $e"
echo "num is $num"
sleep 0.1
done
but it's output was started doing some really weird stuff after 8 as shown here:
1
e is 1
num is 2
2
1
e is 1
num is 3
3
2
1
e is 1
num is 4
4
3
2
1
e is 1
num is 5
5
4
3
2
1
e is 1
num is 6
6
5
4
3
2
1
e is 1
num is 7
7
6
5
4
3
2
1
e is 1
num is 8
8
7
6
5
4
3
2
1
e is 1
num is 9
8
7
6
5
4
3
2
1
9
e is 9
num is 10
8
7
6
5
4
3
2
1
9
10
e is 10
num is 11
8
7
6
5
4
3
2
1
11
9
10
e is 10
num is 12
8
7
6
5
4
3
2
1
11
12
9
10
e is 10
num is 13
8
7
6
5
4
3
2
1
11
12
9
10
13
e is 13
num is 14
8
7
6
5
4
3
2
1
11
12
9
10
13
14
e is 14
num is 15
8
7
6
5
4
3
2
1
11
12
9
10
15
13
14
e is 14
num is 16
8
7
6
5
4
3
2
1
11
12
9
10
15
16
13
14
e is 14
num is 17
8
7
6
5
4
3
2
1
17
11
12
9
10
15
16
13
14
e is 14
num is 18
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
e is 14
num is 19
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
19
e is 19
num is 20
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
20
19
e is 19
num is 21
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
21
20
19
e is 19
num is 22
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
22
21
20
19
e is 19
num is 23
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
23
22
21
20
19
e is 19
num is 24
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
24
23
22
21
20
19
e is 19
num is 25
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
25
24
23
22
21
20
19
e is 19
num is 26
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
26
25
24
23
22
21
20
19
e is 19
num is 27
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
26
25
24
23
22
21
20
19
27
e is 27
num is 28
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
26
25
24
23
22
21
20
19
28
27
e is 27
num is 29
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
26
25
24
23
22
21
20
19
28
27
29
e is 29
num is 30
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
26
25
24
23
22
21
20
19
28
27
29
30
e is 30
num is 31
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
26
25
24
23
22
21
20
19
28
27
29
30
31
e is 31
num is 32
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
26
25
24
23
22
21
20
19
28
27
29
30
31
32
e is 32
num is 33
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
26
25
24
23
22
21
20
19
28
27
33
29
30
31
32
e is 32
num is 34
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
26
25
24
23
22
21
20
19
28
27
33
34
29
30
31
32
e is 32
num is 35
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
26
25
24
23
22
21
20
19
28
27
33
34
35
29
30
31
32
e is 32
num is 36
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
26
25
24
23
22
21
20
19
28
27
33
34
35
36
29
30
31
32
e is 32
num is 37
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
26
25
24
23
22
21
20
19
28
27
33
34
35
36
29
30
31
32
37
e is 37
num is 38
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
26
25
24
23
22
21
20
19
28
27
33
34
35
36
29
30
31
32
37
38
e is 38
num is 39
39
8
7
6
5
4
3
2
1
17
18
11
12
9
10
15
16
13
14
26
25
24
23
22
21
20
19
28
27
33
34
35
36
29
30
31
32
37
38
e is 38
num is 40
^C
can anybody explain this?
command-line bash scripts
command-line bash scripts
edited Mar 31 at 6:08
TheRedStormer
asked Mar 31 at 5:55
TheRedStormerTheRedStormer
165
165
2
You're iterating on associative array keys. Associative arrays aren't necessarily sorted.
– Sergiy Kolodyazhnyy
Mar 31 at 6:13
add a comment |
2
You're iterating on associative array keys. Associative arrays aren't necessarily sorted.
– Sergiy Kolodyazhnyy
Mar 31 at 6:13
2
2
You're iterating on associative array keys. Associative arrays aren't necessarily sorted.
– Sergiy Kolodyazhnyy
Mar 31 at 6:13
You're iterating on associative array keys. Associative arrays aren't necessarily sorted.
– Sergiy Kolodyazhnyy
Mar 31 at 6:13
add a comment |
1 Answer
1
active
oldest
votes
You have declared associative array, that's why output is not ordered. Change declare -A A1
to declare -a A1
to declare indexed array.
Like follows:
#!/bin/bash
num=1
declare -a A1
while true
do
A1[$(($num + 1))]=$num
num=$(( $num + 1 ))
e=0
for e in ${A1[@]}
do
echo $e
done
echo "e is $e"
echo "num is $num"
sleep 0.1
done
Also, your array index is not zero based. First element is A1[2]. To fix it change first line inside loop to A1[$(($num - 1))]=$num
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "89"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1130065%2fwhy-does-this-simple-script-give-such-a-weird-output%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You have declared associative array, that's why output is not ordered. Change declare -A A1
to declare -a A1
to declare indexed array.
Like follows:
#!/bin/bash
num=1
declare -a A1
while true
do
A1[$(($num + 1))]=$num
num=$(( $num + 1 ))
e=0
for e in ${A1[@]}
do
echo $e
done
echo "e is $e"
echo "num is $num"
sleep 0.1
done
Also, your array index is not zero based. First element is A1[2]. To fix it change first line inside loop to A1[$(($num - 1))]=$num
add a comment |
You have declared associative array, that's why output is not ordered. Change declare -A A1
to declare -a A1
to declare indexed array.
Like follows:
#!/bin/bash
num=1
declare -a A1
while true
do
A1[$(($num + 1))]=$num
num=$(( $num + 1 ))
e=0
for e in ${A1[@]}
do
echo $e
done
echo "e is $e"
echo "num is $num"
sleep 0.1
done
Also, your array index is not zero based. First element is A1[2]. To fix it change first line inside loop to A1[$(($num - 1))]=$num
add a comment |
You have declared associative array, that's why output is not ordered. Change declare -A A1
to declare -a A1
to declare indexed array.
Like follows:
#!/bin/bash
num=1
declare -a A1
while true
do
A1[$(($num + 1))]=$num
num=$(( $num + 1 ))
e=0
for e in ${A1[@]}
do
echo $e
done
echo "e is $e"
echo "num is $num"
sleep 0.1
done
Also, your array index is not zero based. First element is A1[2]. To fix it change first line inside loop to A1[$(($num - 1))]=$num
You have declared associative array, that's why output is not ordered. Change declare -A A1
to declare -a A1
to declare indexed array.
Like follows:
#!/bin/bash
num=1
declare -a A1
while true
do
A1[$(($num + 1))]=$num
num=$(( $num + 1 ))
e=0
for e in ${A1[@]}
do
echo $e
done
echo "e is $e"
echo "num is $num"
sleep 0.1
done
Also, your array index is not zero based. First element is A1[2]. To fix it change first line inside loop to A1[$(($num - 1))]=$num
answered Mar 31 at 8:10
LeonidMewLeonidMew
961623
961623
add a comment |
add a comment |
Thanks for contributing an answer to Ask Ubuntu!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1130065%2fwhy-does-this-simple-script-give-such-a-weird-output%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
You're iterating on associative array keys. Associative arrays aren't necessarily sorted.
– Sergiy Kolodyazhnyy
Mar 31 at 6:13