LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-19-2014, 11:29 PM   #1
jags1984
Member
 
Registered: Mar 2013
Posts: 83

Rep: Reputation: Disabled
How to find the index of bash array


How to find the index of the bash array if the index is a string.


Code:
declare -A TEST
TEST["apple"]="Doctor"
TEST["mango"]="Summer"
TEST["pine"]="fine


Here I wont to know what is the index of the first second and third array
 
Old 01-20-2014, 12:01 AM   #2
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 3,338

Rep: Reputation: Disabled
To retrieve all indices, use ${!TEST[@]}. The exclamation mark means you get a list of indices instead of a list of values, which is what ${TEST[@]} would produce. You can use either of these in a for loop:
Code:
declare -A TEST

TEST["apple"]="Doctor"
TEST["mango"]="Summer"
TEST["pine"]="fine"

for x in ${!TEST[@]}; do
  echo Index ${x} points to ${TEST[${x}]}
done
It's important to remember that the ordering of elements in an associate array is not defined. While this array obviously has three index/value pairs, they may not necessarily appear in the order they were created when you iterate through the array.

In other words, you can't ask for the index or the value of the "third" member. You'll have to look for a value corresponding to a certain index.
 
2 members found this post helpful.
Old 01-20-2014, 12:06 AM   #3
jags1984
Member
 
Registered: Mar 2013
Posts: 83

Original Poster
Rep: Reputation: Disabled
Thankyou
 
  


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
How to use a variable to index an array in Bash shell script? anotheremily Programming 3 06-22-2012 09:46 PM
[SOLVED] Compare array index number against element in bash rewtnull Programming 10 11-01-2011 02:53 PM
get index of an element in array in bash mangatmodi Programming 4 11-20-2009 07:45 AM
Bash Script Array index value Kedelfor Programming 10 04-29-2009 04:37 AM
C++ find array index for largest number. joshp Programming 7 03-15-2009 10:56 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:59 AM.

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