On 2011-05-16, no.top.post / gmail.com wrote: > In article <r5g7a8-3a5.ln1 / 206-248-139-163.dsl.teksavvy.com>, "Chris F.A. Johnson" <cfajohnson / gmail.com> wrote: > >> On 2011-05-16, no.top.post / gmail.com wrote: >> > awk &stuff can "give me the the Nth element", >> > but, without writing your own search-loop, >> > what can "give me the index of the 'element' >> > which is <elementValue>" ? >> > >> > I think it's called 'reverse indexing' ? >> >> Do you want 'grep -n'? >> >> If not, please be more specific. > > awk & grep are not typically used on arrays of single chars, > but that's the simplest, to ilustrate the concept. > Given the 6char array: [abcdef] That's not an array; it's a string (this is shell, not C). ><elementValue> means the value of the element. > The first element has value "a". Why not use the correct term, 'character'? Then we would have understood what you meant. > "the index of the 'element' which is "e" is ><the 5th element>; which index is 4 or 5 depending > on whether you count from 0 or 1 respectively. index() { case $1 in *"$2"*) idx=${1%%$2*} echo "$(( ${#idx} + 1 ))" ;; *) echo 0 ;; esac } $ index abcdef c 3 -- Chris F.A. Johnson, <http://cfajohnson.com> Author: Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress) Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)