LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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 11-19-2009, 08:23 AM   #1
mangatmodi
Member
 
Registered: Aug 2009
Location: India
Distribution: Fedora 12
Posts: 35

Rep: Reputation: 0
get index of an element in array in bash


I have an array say
Code:
array_name=( XXX YYY ZZZ aaa bbb ccc)
and I want to get Index of aaa. Is there any function or a simple way, other than looping, to get Index of an element of an array.

I don't want to use loop, since it is a part of a larger program and looping will really make it complex

I'm using GNU bash, version 3.2.33(1)-release (i386-redhat-linux-gnu)

Last edited by mangatmodi; 11-19-2009 at 08:30 AM. Reason: Missing Information
 
Old 11-19-2009, 08:51 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,696
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Code:
echo ${!array_name[@]}
 
Old 11-19-2009, 09:01 AM   #3
mangatmodi
Member
 
Registered: Aug 2009
Location: India
Distribution: Fedora 12
Posts: 35

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by ghostdog74 View Post
Code:
echo ${!array_name[@]}
@ghostdog74

Sir thank you for your reply but I still didn't get it. The above code give me following output
Code:
0 1 2 3 4 5
But how can I deduce from it that 3 is the Index of aaa ?
 
Old 11-19-2009, 06:46 PM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,696
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
do it loop. Its really not that complex. after you done it, make a function out of it, so that next time, you can use it again
 
Old 11-20-2009, 07:45 AM   #5
mangatmodi
Member
 
Registered: Aug 2009
Location: India
Distribution: Fedora 12
Posts: 35

Original Poster
Rep: Reputation: 0
Thank you Sir,

This is second time you are helping me in this week sir.
I had solved the problem in the same manner before your last post though :P

Once again, I'm really grateful to you
 
  


Reply

Tags
array, bash, shell scripting


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
Bash Script Array index value Kedelfor Programming 10 04-29-2009 04:37 AM
[perl] copying an array element into another array s0l1dsnak3123 Programming 2 05-17-2008 01:47 AM
index of an element in the array ? thelonius Programming 1 09-24-2005 12:41 PM
return index of element in java multidimensional array dave bean Programming 8 11-28-2003 11:00 AM
C++ Six Element Array petercool Programming 2 08-20-2003 11:08 AM


All times are GMT -5. The time now is 02:14 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration