From: Robert Klemme Date: 2011-05-17T16:14:51+09:00 Subject: Re: Re (2): Linux utility with reverse index facility? On Mon, May 16, 2011 at 9:40 PM, wrote: > In article , "Chris F.A. Johnson" 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 " ? >> > >> > 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] > means the value of the element. > The first element has value "a". > "the  index of the 'element' which is "e" is > ; which index is  4 or 5 depending > on whether you count from 0 or 1 respectively. > > So what did I mean by "writing your own search-loop"? > > AFAIK ruby has >. > That mean ruby has structures which operate like association > memories; so you can ask "what's the index of the element > which has value "dog", in the 'structure' of strings. irb(main):001:0> h = {'foo' => 'dog', 'bar' => 'cat'} => {"foo"=>"dog", "bar"=>"cat"} irb(main):002:0> h.rassoc 'dog' => ["foo", "dog"] irb(main):003:0> a = h.to_a => [["foo", "dog"], ["bar", "cat"]] irb(main):004:0> a.rassoc 'dog' => ["foo", "dog"] Please also have a look at the documentation (either "ri" or http://ruby-doc.org/). Cheers robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/