Synchronized combination of for loops' output
up vote
1
down vote
favorite
I have two for loops:
for i in 0 1 2 3; do echo $i;done
for j in a b c d; do echo $j;done
0
1
2
3
a
b
c
d
I want the output be:
0
a
1
b
2
c
3
d
How to do it?
command-line bash
add a comment |
up vote
1
down vote
favorite
I have two for loops:
for i in 0 1 2 3; do echo $i;done
for j in a b c d; do echo $j;done
0
1
2
3
a
b
c
d
I want the output be:
0
a
1
b
2
c
3
d
How to do it?
command-line bash
Looks like a nested loop is required.
– Graham
Nov 18 at 14:05
Looks that with nested loop I will get 0 a b c d; 1 a b c d etc. 16 times
– Josef Klimuk
Nov 18 at 14:12
Do you need it to be more complex than justfor i in 0 a 1 b 2 c 3 d
?
– wjandrea
Nov 18 at 14:18
@wjandrea, perlduck already pointed on it. Right, it is my fault, I need it to be more complex, since I want to use outputs of different loops, not just echoing the range...
– Josef Klimuk
Nov 18 at 14:34
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have two for loops:
for i in 0 1 2 3; do echo $i;done
for j in a b c d; do echo $j;done
0
1
2
3
a
b
c
d
I want the output be:
0
a
1
b
2
c
3
d
How to do it?
command-line bash
I have two for loops:
for i in 0 1 2 3; do echo $i;done
for j in a b c d; do echo $j;done
0
1
2
3
a
b
c
d
I want the output be:
0
a
1
b
2
c
3
d
How to do it?
command-line bash
command-line bash
asked Nov 18 at 13:52
Josef Klimuk
551114
551114
Looks like a nested loop is required.
– Graham
Nov 18 at 14:05
Looks that with nested loop I will get 0 a b c d; 1 a b c d etc. 16 times
– Josef Klimuk
Nov 18 at 14:12
Do you need it to be more complex than justfor i in 0 a 1 b 2 c 3 d
?
– wjandrea
Nov 18 at 14:18
@wjandrea, perlduck already pointed on it. Right, it is my fault, I need it to be more complex, since I want to use outputs of different loops, not just echoing the range...
– Josef Klimuk
Nov 18 at 14:34
add a comment |
Looks like a nested loop is required.
– Graham
Nov 18 at 14:05
Looks that with nested loop I will get 0 a b c d; 1 a b c d etc. 16 times
– Josef Klimuk
Nov 18 at 14:12
Do you need it to be more complex than justfor i in 0 a 1 b 2 c 3 d
?
– wjandrea
Nov 18 at 14:18
@wjandrea, perlduck already pointed on it. Right, it is my fault, I need it to be more complex, since I want to use outputs of different loops, not just echoing the range...
– Josef Klimuk
Nov 18 at 14:34
Looks like a nested loop is required.
– Graham
Nov 18 at 14:05
Looks like a nested loop is required.
– Graham
Nov 18 at 14:05
Looks that with nested loop I will get 0 a b c d; 1 a b c d etc. 16 times
– Josef Klimuk
Nov 18 at 14:12
Looks that with nested loop I will get 0 a b c d; 1 a b c d etc. 16 times
– Josef Klimuk
Nov 18 at 14:12
Do you need it to be more complex than just
for i in 0 a 1 b 2 c 3 d
?– wjandrea
Nov 18 at 14:18
Do you need it to be more complex than just
for i in 0 a 1 b 2 c 3 d
?– wjandrea
Nov 18 at 14:18
@wjandrea, perlduck already pointed on it. Right, it is my fault, I need it to be more complex, since I want to use outputs of different loops, not just echoing the range...
– Josef Klimuk
Nov 18 at 14:34
@wjandrea, perlduck already pointed on it. Right, it is my fault, I need it to be more complex, since I want to use outputs of different loops, not just echoing the range...
– Josef Klimuk
Nov 18 at 14:34
add a comment |
2 Answers
2
active
oldest
votes
up vote
5
down vote
accepted
You could define two arrays of the same length with the values you want to iterate over first. Then you can iterate over the indexes of one of the arrays and look them up in both:
arr1=(0 1 2 3)
arr2=(a b c d)
for i in ${!arr1[@]}; do
echo ${arr1[i]}
echo ${arr2[i]}
done
# Output below:
0
a
1
b
2
c
3
d
Defining arrays works by assigning a list of elements in parentheses, as you see on the first two lines.
The for-loop iterates over ${!arr1[@]}
, which returns a list of all ([@]
) indexes (!
) of the array variable arr1
. This can be used here because we assume that both arr1
and arr2
have exactly the same indexes, because they were defined with the same number of elements. In theory, arrays could have different lengths or even have "holes" i.e. unassigned indexes in between, but both is not the case here, so we don't care for it.
Accessing a specific element value of an array at the position defined by the value of the variable $i
works by typing ${arr1[i]}
.
add a comment |
up vote
3
down vote
Another possibility would be to use an associative array (aka hash aka dictionary) and then iterate over it and retrieving keys and values respectively:
#!/usr/bin/env bash
declare -A items=(
[0]=a
[1]=b
[2]=c
[3]=d
[four]=eeh
);
for key in "${!items[@]}"; do
echo "key : $key";
echo "value: ${items[$key]}";
done
Output:
key : 0
value: a
key : 1
value: b
key : 2
value: c
key : 3
value: d
key : four
value: eeh
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
accepted
You could define two arrays of the same length with the values you want to iterate over first. Then you can iterate over the indexes of one of the arrays and look them up in both:
arr1=(0 1 2 3)
arr2=(a b c d)
for i in ${!arr1[@]}; do
echo ${arr1[i]}
echo ${arr2[i]}
done
# Output below:
0
a
1
b
2
c
3
d
Defining arrays works by assigning a list of elements in parentheses, as you see on the first two lines.
The for-loop iterates over ${!arr1[@]}
, which returns a list of all ([@]
) indexes (!
) of the array variable arr1
. This can be used here because we assume that both arr1
and arr2
have exactly the same indexes, because they were defined with the same number of elements. In theory, arrays could have different lengths or even have "holes" i.e. unassigned indexes in between, but both is not the case here, so we don't care for it.
Accessing a specific element value of an array at the position defined by the value of the variable $i
works by typing ${arr1[i]}
.
add a comment |
up vote
5
down vote
accepted
You could define two arrays of the same length with the values you want to iterate over first. Then you can iterate over the indexes of one of the arrays and look them up in both:
arr1=(0 1 2 3)
arr2=(a b c d)
for i in ${!arr1[@]}; do
echo ${arr1[i]}
echo ${arr2[i]}
done
# Output below:
0
a
1
b
2
c
3
d
Defining arrays works by assigning a list of elements in parentheses, as you see on the first two lines.
The for-loop iterates over ${!arr1[@]}
, which returns a list of all ([@]
) indexes (!
) of the array variable arr1
. This can be used here because we assume that both arr1
and arr2
have exactly the same indexes, because they were defined with the same number of elements. In theory, arrays could have different lengths or even have "holes" i.e. unassigned indexes in between, but both is not the case here, so we don't care for it.
Accessing a specific element value of an array at the position defined by the value of the variable $i
works by typing ${arr1[i]}
.
add a comment |
up vote
5
down vote
accepted
up vote
5
down vote
accepted
You could define two arrays of the same length with the values you want to iterate over first. Then you can iterate over the indexes of one of the arrays and look them up in both:
arr1=(0 1 2 3)
arr2=(a b c d)
for i in ${!arr1[@]}; do
echo ${arr1[i]}
echo ${arr2[i]}
done
# Output below:
0
a
1
b
2
c
3
d
Defining arrays works by assigning a list of elements in parentheses, as you see on the first two lines.
The for-loop iterates over ${!arr1[@]}
, which returns a list of all ([@]
) indexes (!
) of the array variable arr1
. This can be used here because we assume that both arr1
and arr2
have exactly the same indexes, because they were defined with the same number of elements. In theory, arrays could have different lengths or even have "holes" i.e. unassigned indexes in between, but both is not the case here, so we don't care for it.
Accessing a specific element value of an array at the position defined by the value of the variable $i
works by typing ${arr1[i]}
.
You could define two arrays of the same length with the values you want to iterate over first. Then you can iterate over the indexes of one of the arrays and look them up in both:
arr1=(0 1 2 3)
arr2=(a b c d)
for i in ${!arr1[@]}; do
echo ${arr1[i]}
echo ${arr2[i]}
done
# Output below:
0
a
1
b
2
c
3
d
Defining arrays works by assigning a list of elements in parentheses, as you see on the first two lines.
The for-loop iterates over ${!arr1[@]}
, which returns a list of all ([@]
) indexes (!
) of the array variable arr1
. This can be used here because we assume that both arr1
and arr2
have exactly the same indexes, because they were defined with the same number of elements. In theory, arrays could have different lengths or even have "holes" i.e. unassigned indexes in between, but both is not the case here, so we don't care for it.
Accessing a specific element value of an array at the position defined by the value of the variable $i
works by typing ${arr1[i]}
.
answered Nov 18 at 14:12
Byte Commander
62.1k26167279
62.1k26167279
add a comment |
add a comment |
up vote
3
down vote
Another possibility would be to use an associative array (aka hash aka dictionary) and then iterate over it and retrieving keys and values respectively:
#!/usr/bin/env bash
declare -A items=(
[0]=a
[1]=b
[2]=c
[3]=d
[four]=eeh
);
for key in "${!items[@]}"; do
echo "key : $key";
echo "value: ${items[$key]}";
done
Output:
key : 0
value: a
key : 1
value: b
key : 2
value: c
key : 3
value: d
key : four
value: eeh
add a comment |
up vote
3
down vote
Another possibility would be to use an associative array (aka hash aka dictionary) and then iterate over it and retrieving keys and values respectively:
#!/usr/bin/env bash
declare -A items=(
[0]=a
[1]=b
[2]=c
[3]=d
[four]=eeh
);
for key in "${!items[@]}"; do
echo "key : $key";
echo "value: ${items[$key]}";
done
Output:
key : 0
value: a
key : 1
value: b
key : 2
value: c
key : 3
value: d
key : four
value: eeh
add a comment |
up vote
3
down vote
up vote
3
down vote
Another possibility would be to use an associative array (aka hash aka dictionary) and then iterate over it and retrieving keys and values respectively:
#!/usr/bin/env bash
declare -A items=(
[0]=a
[1]=b
[2]=c
[3]=d
[four]=eeh
);
for key in "${!items[@]}"; do
echo "key : $key";
echo "value: ${items[$key]}";
done
Output:
key : 0
value: a
key : 1
value: b
key : 2
value: c
key : 3
value: d
key : four
value: eeh
Another possibility would be to use an associative array (aka hash aka dictionary) and then iterate over it and retrieving keys and values respectively:
#!/usr/bin/env bash
declare -A items=(
[0]=a
[1]=b
[2]=c
[3]=d
[four]=eeh
);
for key in "${!items[@]}"; do
echo "key : $key";
echo "value: ${items[$key]}";
done
Output:
key : 0
value: a
key : 1
value: b
key : 2
value: c
key : 3
value: d
key : four
value: eeh
answered Nov 18 at 15:03
PerlDuck
4,83111130
4,83111130
add a comment |
add a comment |
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%2f1093961%2fsynchronized-combination-of-for-loops-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
Looks like a nested loop is required.
– Graham
Nov 18 at 14:05
Looks that with nested loop I will get 0 a b c d; 1 a b c d etc. 16 times
– Josef Klimuk
Nov 18 at 14:12
Do you need it to be more complex than just
for i in 0 a 1 b 2 c 3 d
?– wjandrea
Nov 18 at 14:18
@wjandrea, perlduck already pointed on it. Right, it is my fault, I need it to be more complex, since I want to use outputs of different loops, not just echoing the range...
– Josef Klimuk
Nov 18 at 14:34