LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-21-2005, 10:21 AM   #1
Kedelfor
Member
 
Registered: Jan 2004
Posts: 49

Rep: Reputation: 15
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
 
Old 05-21-2005, 10:43 AM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
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

Last edited by keefaz; 05-21-2005 at 10:44 AM.
 
Old 05-21-2005, 11:56 AM   #3
Kedelfor
Member
 
Registered: Jan 2004
Posts: 49

Original Poster
Rep: Reputation: 15
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.
 
Old 05-21-2005, 12:42 PM   #4
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
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
 
Old 05-21-2005, 12:55 PM   #5
Kedelfor
Member
 
Registered: Jan 2004
Posts: 49

Original Poster
Rep: Reputation: 15
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???
 
Old 05-21-2005, 01:05 PM   #6
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
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
 
Old 05-21-2005, 01:35 PM   #7
Kedelfor
Member
 
Registered: Jan 2004
Posts: 49

Original Poster
Rep: Reputation: 15
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.
 
Old 10-20-2008, 03:39 PM   #8
mphadnis
LQ Newbie
 
Registered: Oct 2008
Posts: 1

Rep: Reputation: 0
Kedelfor,

were you able to find a solution to your question? I need to do the exact same thing, TIA
 
Old 11-11-2008, 08:39 AM   #9
atelszewski
Member
 
Registered: Aug 2007
Distribution: Slackware
Posts: 948

Rep: Reputation: Disabled
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
 
1 members found this post helpful.
Old 04-18-2009, 10:30 PM   #10
pooryorick
LQ Newbie
 
Registered: Aug 2007
Posts: 10

Rep: Reputation: 3
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
 
Old 04-29-2009, 04:37 AM   #11
14341
LQ Newbie
 
Registered: Apr 2009
Posts: 1

Rep: Reputation: 0
i think one needs to enable the extended pattern matching using
Code:
shopt -s extglob
for the above specified code to work.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash shell script split array robertngo Programming 13 06-19-2011 11:01 PM
index of an element in the array ? thelonius Programming 1 09-24-2005 12:41 PM
Bash script text line into an array toolshed Programming 1 06-13-2005 05:49 PM
Bash script to change a filename associated with an inode index number. Ziv Programming 22 06-19-2004 08:41 AM
MAJOR problem ... bash script array HELP !!!!! michael_util Slackware 1 02-13-2004 06:51 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:45 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration