LinuxQuestions.org
Help answer threads with 0 replies.
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-20-2014, 12:29 AM   #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, 01:01 AM   #2
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 2,404

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, 01:06 AM   #3
jags1984
Member
 
Registered: Mar 2013
Posts: 83

Original Poster
Rep: Reputation: Disabled
Thankyou
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 10:46 PM
[SOLVED] Compare array index number against element in bash rewtnull Programming 10 11-01-2011 03:53 PM
get index of an element in array in bash mangatmodi Programming 4 11-20-2009 08:45 AM
Bash Script Array index value Kedelfor Programming 10 04-29-2009 05:37 AM
C++ find array index for largest number. joshp Programming 7 03-15-2009 11:56 AM


All times are GMT -5. The time now is 04:32 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration