Return the index of an element which matches a value
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I have an array -
val=(4196976 4601313 4242010 0 3581283 2392831 3176852 3205880 3794451 0 3627872)
I am trying to find the index of all the elements whose values are equal to 0
How can i achienve this?
This is what i have tried -
for ((i = 1; i <= 10; i++)); do
if [ "$i" -eq "0" ]; then
echo "Index: $i, value: $val[i]"
fi
done
The output should be
Index: 3, value: 0
Index: 9, value: 0
linux bash
add a comment |
up vote
0
down vote
favorite
I have an array -
val=(4196976 4601313 4242010 0 3581283 2392831 3176852 3205880 3794451 0 3627872)
I am trying to find the index of all the elements whose values are equal to 0
How can i achienve this?
This is what i have tried -
for ((i = 1; i <= 10; i++)); do
if [ "$i" -eq "0" ]; then
echo "Index: $i, value: $val[i]"
fi
done
The output should be
Index: 3, value: 0
Index: 9, value: 0
linux bash
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have an array -
val=(4196976 4601313 4242010 0 3581283 2392831 3176852 3205880 3794451 0 3627872)
I am trying to find the index of all the elements whose values are equal to 0
How can i achienve this?
This is what i have tried -
for ((i = 1; i <= 10; i++)); do
if [ "$i" -eq "0" ]; then
echo "Index: $i, value: $val[i]"
fi
done
The output should be
Index: 3, value: 0
Index: 9, value: 0
linux bash
I have an array -
val=(4196976 4601313 4242010 0 3581283 2392831 3176852 3205880 3794451 0 3627872)
I am trying to find the index of all the elements whose values are equal to 0
How can i achienve this?
This is what i have tried -
for ((i = 1; i <= 10; i++)); do
if [ "$i" -eq "0" ]; then
echo "Index: $i, value: $val[i]"
fi
done
The output should be
Index: 3, value: 0
Index: 9, value: 0
linux bash
linux bash
asked 22 hours ago
PepeHands
51
51
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
accepted
#!/bin/bash
val=(4196976 4601313 4242010 0 3581283 2392831 3176852 3205880 3794451 0 3627872)
n=0
for i in $val[@]; do
[ $i -eq 0 ] && echo Index: $n, value: $i
((n++))
done
add a comment |
up vote
2
down vote
If the array has gaps in it, it might be better to loop over the indices of the array instead:
for i in "$!val[@]"
do
if [[ $val[i] -eq 0 ]]
then
echo "Index: $i, value: $val[i]"
fi
done
So, if your array was like:
val=([100]=327823 [54]=0 [787998]=377463287)
You'd still get 54
as the index.
This should also work for associative arrays (strings as indices instead of integers).
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
#!/bin/bash
val=(4196976 4601313 4242010 0 3581283 2392831 3176852 3205880 3794451 0 3627872)
n=0
for i in $val[@]; do
[ $i -eq 0 ] && echo Index: $n, value: $i
((n++))
done
add a comment |
up vote
0
down vote
accepted
#!/bin/bash
val=(4196976 4601313 4242010 0 3581283 2392831 3176852 3205880 3794451 0 3627872)
n=0
for i in $val[@]; do
[ $i -eq 0 ] && echo Index: $n, value: $i
((n++))
done
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
#!/bin/bash
val=(4196976 4601313 4242010 0 3581283 2392831 3176852 3205880 3794451 0 3627872)
n=0
for i in $val[@]; do
[ $i -eq 0 ] && echo Index: $n, value: $i
((n++))
done
#!/bin/bash
val=(4196976 4601313 4242010 0 3581283 2392831 3176852 3205880 3794451 0 3627872)
n=0
for i in $val[@]; do
[ $i -eq 0 ] && echo Index: $n, value: $i
((n++))
done
answered 21 hours ago
Ipor Sircer
9,97211023
9,97211023
add a comment |
add a comment |
up vote
2
down vote
If the array has gaps in it, it might be better to loop over the indices of the array instead:
for i in "$!val[@]"
do
if [[ $val[i] -eq 0 ]]
then
echo "Index: $i, value: $val[i]"
fi
done
So, if your array was like:
val=([100]=327823 [54]=0 [787998]=377463287)
You'd still get 54
as the index.
This should also work for associative arrays (strings as indices instead of integers).
add a comment |
up vote
2
down vote
If the array has gaps in it, it might be better to loop over the indices of the array instead:
for i in "$!val[@]"
do
if [[ $val[i] -eq 0 ]]
then
echo "Index: $i, value: $val[i]"
fi
done
So, if your array was like:
val=([100]=327823 [54]=0 [787998]=377463287)
You'd still get 54
as the index.
This should also work for associative arrays (strings as indices instead of integers).
add a comment |
up vote
2
down vote
up vote
2
down vote
If the array has gaps in it, it might be better to loop over the indices of the array instead:
for i in "$!val[@]"
do
if [[ $val[i] -eq 0 ]]
then
echo "Index: $i, value: $val[i]"
fi
done
So, if your array was like:
val=([100]=327823 [54]=0 [787998]=377463287)
You'd still get 54
as the index.
This should also work for associative arrays (strings as indices instead of integers).
If the array has gaps in it, it might be better to loop over the indices of the array instead:
for i in "$!val[@]"
do
if [[ $val[i] -eq 0 ]]
then
echo "Index: $i, value: $val[i]"
fi
done
So, if your array was like:
val=([100]=327823 [54]=0 [787998]=377463287)
You'd still get 54
as the index.
This should also work for associative arrays (strings as indices instead of integers).
answered 21 hours ago
Olorin
1,479112
1,479112
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f481375%2freturn-the-index-of-an-element-which-matches-a-value%23new-answer', 'question_page');
);
Post as a guest
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
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
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