LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash Script Array index value (https://www.linuxquestions.org/questions/programming-9/bash-script-array-index-value-325682/)

Kedelfor 05-21-2005 10:21 AM

Bash Script Array index value
 
Is there any way to get the value that is in the array index. ie

array[1]=one
array[2]=two
array[3]=three

That would be an array and I want the index values to be printed as well not just the value of the array.

Thanks

keefaz 05-21-2005 10:43 AM

Something like :
Code:

#!/bin/bash

array=(one two three)
count=${#array[@]}
index=0
while [ "$index" -lt "$count" ]; do
    echo -e "index: $index\tvalue: ${array[$index]}"
    let "index++"
done

See :
http://linuxreviews.org/beginner/Bas.../en/x4945.html
http://www.die.net/doc/linux/abs-guide/arrays.html

Kedelfor 05-21-2005 11:56 AM

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.

keefaz 05-21-2005 12:42 PM

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


Kedelfor 05-21-2005 12:55 PM

All I really want to do is know if i can print the number. I will show you what i mean

I have a number sequence like

server[101]=Server 1
server[201]=Server 1
server[301]=Server 1
server[401]=Server 1
server[501]=Server 1

Not in numeric order

they would reference somehting like

101=Site1
201=site2
etc...

I was wondering if i could just echo the index number so i can reference to it like if ${server[ID]} so i can say like

Server 1 at Site1

Does this make sense at all???

keefaz 05-21-2005 01:05 PM

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

Kedelfor 05-21-2005 01:35 PM

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.

mphadnis 10-20-2008 03:39 PM

Kedelfor,

were you able to find a solution to your question? I need to do the exact same thing, TIA

atelszewski 11-11-2008 08:39 AM

Hello,

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

Best regards,
atelszewski

pooryorick 04-18-2009 10:30 PM

not compatible with earlier versions
 
note that the syntax used by atelszewski did not exist in earlier versions of bash.

--
Poor Yorick
www.pooryorick.com

14341 04-29-2009 04:37 AM

i think one needs to enable the extended pattern matching using
Code:

shopt -s extglob
for the above specified code to work.


All times are GMT -5. The time now is 07:42 PM.