bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Locate Element Matching Search Pattern in Array Variable


From: Hon-Chi Ng
Subject: Locate Element Matching Search Pattern in Array Variable
Date: Tue, 24 Jul 2001 14:12:35 -0700

Hi

I hope this is the right place to ask questions about bash usage, as
the name suggesting bug report.  I apologize if this is not the proper
place to do so, and please kindly direct me to the right place.  BTW,
is the news:gnu.bash.bug Usenet same as bug-bash@gnu.org?

I'm looking for Bash syntax/construct that can locate an element (the
first matching) in an array variable for a given search pattern.
Basically, the array variable and search pattern are given, and either
the subscript or the element is expected if found, or either -1 or
null if not found.  For illustration, it will be something similar to
lsearch in Tcl & grep in Perl.

Before I go off and write a function with for loop to do so, I'm
interested in a more efficient and concise way in achieving it.

For example, I can find out if at least an element matching the search
pattern exists in a given array variable by using [[ == ]] construct
as shown below, without defining a function to loop thru the array
variable.

E.g. True if element not prefixed with - exists in foo[]
  if [[ " ${foo[*]} " == *" "!(-*)" "* ]]; then ...; else ...; fi

However, I don't know how to get the element (or its subscript) that
matches the search pattern, without writing an elaborate function.

Thanks.  I appreciate your help.

Regards
Hon-Chi


------------------------------------------------------------
--== Sent via Deja.com ==--
http://www.deja.com/



reply via email to

[Prev in Thread] Current Thread [Next in Thread]