ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Well what I forgot to mention was that I am using a wierd scheme for my index numbers. They are kind of like reference numbers, hence why i would want to know what they are so I can use what the refernce to in my scripts.
I think you will get better result with searching the index in the array
from a value reference like :
Code:
#!/bin/bash
function search_array() {
index=0
while [ "$index" -lt "${#myArray[@]}" ]; do
if [ "${myArray[$index]}" = "$1" ]; then
echo $index
return
fi
let "index++"
done
echo ""
}
myArray=(one two three)
value="one"
index=$(search_array $value)
if [ -z "$index" ]; then
echo -e "the value \"$value\" is not in \$myArray"
else
echo -e "the value \"$value\" was found at index: $index"
fi
Well you could echo just the index number with my function above,
just replace 'myArray' with 'server', although it won't work wiith
arrays that contains multiple same values...
Now if there are some built-in or shorter functions to echo the index
of a bash array, I will like to see it
Thank you for all your input, I know witch some other languages you can just output the index name. i wasn't sure if bash had that or not. Thanks again for your help.
I found this construct to solve your problem: ${!array[*]}
Code:
#!/bin/sh
array[5]="tom"
array[13]="jenny"
array[77]="julie"
echo ${!array[*]} # find all the indexes
# print all indexes and associated values
for I in ${!array[*]}; do
echo $I: ${array[$I]}
done
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.