Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #4660

Re: Re (2): Linux utility with reverse index facility?

From Robert Klemme <shortcutter@googlemail.com>
Newsgroups comp.lang.ruby
Subject Re: Re (2): Linux utility with reverse index facility?
Date 2011-05-17 02:14 -0500
Organization Service de news de lacave.net
Message-ID <BANLkTinHRHPQB2TFfSMSdFxGd-1ecJNW0Q@mail.gmail.com> (permalink)
References <r5g7a8-3a5.ln1@206-248-139-163.dsl.teksavvy.com> <iqru16$5cq$1@dont-email.me>

Show all headers | View raw


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' ?
>>
>>    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]
> <elementValue> means the value of the element.
> The first element has value "a".
> "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.
>
> 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 = {'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/

Back to comp.lang.ruby | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Linux utility with reverse index facility? no.top.post@gmail.com - 2011-05-16 10:38 +0000
  Re: Linux utility with reverse index facility? pk <pk@pk.invalid> - 2011-05-16 11:41 +0100
  Re: Linux utility with reverse index facility? Robert Klemme <shortcutter@googlemail.com> - 2011-05-16 05:45 -0500
  Re: Linux utility with reverse index facility? "Chris F.A. Johnson" <cfajohnson@gmail.com> - 2011-05-16 07:59 -0400
    Re (2): Linux utility with reverse index facility? no.top.post@gmail.com - 2011-05-16 19:30 +0000
      Re: Re (2): Linux utility with reverse index facility? "Chris F.A. Johnson" <cfajohnson@gmail.com> - 2011-05-16 15:59 -0400
      Re: Re (2): Linux utility with reverse index facility? Robert Klemme <shortcutter@googlemail.com> - 2011-05-17 02:14 -0500
  Re: Linux utility with reverse index facility? David Brown <david@westcontrol.removethisbit.com> - 2011-05-16 14:38 +0200
    Re: Linux utility with reverse index facility? Robert Klemme <shortcutter@googlemail.com> - 2011-05-16 08:10 -0500
  Re: Linux utility with reverse index facility? Janis Papanagnou <janis_papanagnou@hotmail.com> - 2011-05-16 15:08 +0200
  Re: Linux utility with reverse index facility? Stu <stu@rubyprogrammer.net> - 2011-05-16 12:52 -0500
  Re: Linux utility with reverse index facility? Robert Nichols <SEE_SIGNATURE@localhost.localdomain.invalid> - 2011-05-17 22:40 -0500
    Re: Linux utility with reverse index facility? Luuk <Luuk@invalid.lan> - 2011-05-18 19:14 +0200
    Re: Linux utility with reverse index facility? Ed Morton <mortonspam@gmail.com> - 2011-05-19 16:15 -0700
  Re: Linux utility with reverse index facility? Brian Candler <b.candler@pobox.com> - 2011-05-18 03:14 -0500

csiph-web