On Mon, May 16, 2011 at 9:40 PM,  <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' ?
>>
>> =A0 =A0Do you want 'grep -n'?
>>
>> =A0 =A0If 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]
> <elementValue> means the value of the element.
> The first element has value "a".
> "the =A0index of the 'element' which is "e" is
> <the 5th element>; which index is =A04 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 <assocoation <memory-structures>>.
> 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 =3D {'foo' =3D> 'dog', 'bar' =3D> 'cat'}
=3D> {"foo"=3D>"dog", "bar"=3D>"cat"}
irb(main):002:0> h.rassoc 'dog'
=3D> ["foo", "dog"]
irb(main):003:0> a =3D h.to_a
=3D> [["foo", "dog"], ["bar", "cat"]]
irb(main):004:0> a.rassoc 'dog'
=3D> ["foo", "dog"]

Please also have a look at the documentation (either "ri" or
http://ruby-doc.org/).

Cheers

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/