Array index question

Discussion in 'Ruby' started by John Smith, Feb 25, 2010.

  1. John Smith

    John Smith Guest

    Question about an array. Say I have the following array...

    textlist = ["Apple", "Orange", "Lemon", "Grape", "Orange", "Melon",
    "Orange", "Banana"]

    if I did textlist.index("Orage"), I would get "1" returned.

    Can anyone tell me how I could retrieve the index number of the 2nd
    instance of "Orange"?

    Thanks in advance!
    --
    Posted via http://www.ruby-forum.com/.
     
    John Smith, Feb 25, 2010
    #1
    1. Advertising

  2. On Feb 25, 2010, at 11:12 AM, John Smith wrote:
    > Question about an array. Say I have the following array...
    >
    > textlist = ["Apple", "Orange", "Lemon", "Grape", "Orange", "Melon",
    > "Orange", "Banana"]
    >
    > if I did textlist.index("Orage"), I would get "1" returned.

    "Orage" #=> nil
    "Orange" #=> 1 ;-)

    >
    > Can anyone tell me how I could retrieve the index number of the 2nd
    > instance of "Orange"?
    >
    > Thanks in advance!


    Well, I thought this was a simple answer, but I was remembering
    String#index(string, offset)

    something like this:

    def textlist.where_is(target)
    locations = []
    each_with_index {|e,i| locations << i if target === e }
    return nil if locations.empty?
    locations
    end

    textlist.where_is("Orange") #=> [1,4,6]
    textlist.where_is("Cherry") #=> nil
    textlist.where_is("Grape") #=> [3]

    Define it on Array if you want or in a module to extend any object you
    want.

    -Rob

    Rob Biedenharn http://agileconsultingllc.com
     
    Rob Biedenharn, Feb 25, 2010
    #2
    1. Advertising

  3. Please excuse my newbieness, second day with Ruby.

    textlist = ["Apple", "Orange", "Lemon", "Grape", "Orange",
    "Melon","Orange", "Banana"]

    i = textlist.index("Orange")

    if !i.nil?
    puts "textlist[" << i.to_s << "] is \"" << textlist << "\""
    j = textlist[i+1,textlist.length-i-1].index("Orange")+i+1
    if !j.nil?
    puts "textlist[" << j.to_s << "] is \"" << textlist[j] << "\""
    end
    end


    outputs:


    textlist[1] is "Orange"
    textlist[4] is "Orange"
    --
    Posted via http://www.ruby-forum.com/.
     
    David Springer, Feb 25, 2010
    #3
  4. John Smith

    Luc Heinrich Guest

    On 25 f=E9vr. 2010, at 19:20, David Springer wrote:

    > textlist =3D ["Apple", "Orange", "Lemon", "Grape", "Orange",=20
    > "Melon","Orange", "Banana"]
    >=20
    > i =3D textlist.index("Orange")
    >=20
    > if !i.nil?
    > puts "textlist[" << i.to_s << "] is \"" << textlist << "\""
    > j =3D textlist[i+1,textlist.length-i-1].index("Orange")+i+1
    > if !j.nil?
    > puts "textlist[" << j.to_s << "] is \"" << textlist[j] << =

    "\""
    > end
    > end


    class Array
    def indexes_of(obj)
    indexes =3D Array.new
    self.each_with_index {|s,i| indexes << i if s =3D=3D=3D obj }
    return indexes
    end
    end

    textlist =3D ["Apple", "Orange", "Lemon", "Grape", "Orange", =
    "Melon","Orange", "Banana"]
    p textlist.indexes_of("Orange")

    #=3D> [1,4,6]

    --=20
    Luc Heinrich -
     
    Luc Heinrich, Feb 25, 2010
    #4
  5. after some inspiration from Luc I was able to come up with this:

    textlist = ["Apple", "Orange", "Lemon", "Grape", "Orange",

    (0..textlist.length-1).select {|i| textlist == "Orange"}[1]



    --
    Posted via http://www.ruby-forum.com/.
     
    David Springer, Feb 25, 2010
    #5
  6. David Springer wrote:
    > after some inspiration from Luc I was able to come up with this:
    >
    > textlist = ["Apple", "Orange", "Lemon", "Grape", "Orange",
    >
    > (0..textlist.length-1).select {|i| textlist == "Orange"}[1]


    Second day with Ruby, huh...
    --
    Posted via http://www.ruby-forum.com/.
     
    Paul Harrington, Feb 25, 2010
    #6
  7. John Smith

    Marc Heiler Guest

    Marc Heiler, Feb 25, 2010
    #7
  8. John Smith

    Sven Schott Guest

    [Note: parts of this message were removed to make it a legal post.]

    I like Luc's but ever since I got hit with inject:

    class Array
    def indices_of(obj)
    self.inject([]) { |arr, element| arr << element if element == obj; arr
    }
    end
    end

    And I like indices not because I'm language nazi but because of personal
    preference. :)

    On Fri, Feb 26, 2010 at 10:35 AM, Marc Heiler <>wrote:

    > > Second day with Ruby, huh...

    >
    > Ruby simplifies thinking.
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >
     
    Sven Schott, Feb 26, 2010
    #8
  9. in order to simplify ...
    (0...textlist.length).select {|i| textlist == "Orange"}[1]

    David Springer wrote:
    > (0..textlist.length-1).select {|i| textlist == "Orange"}[1]


    --
    Posted via http://www.ruby-forum.com/.
     
    Giampiero Zanchi, Feb 26, 2010
    #9
  10. 2010/2/26 Giampiero Zanchi <>:
    > in order to simplify ...
    > (0...textlist.length).select {|i| textlist == "Orange"}[1]
    >
    > David Springer wrote:
    >> (0..textlist.length-1).select {|i| textlist == "Orange"}[1]


    Interesting approach. That could also be done with

    irb(main):005:0> textlist.size.times.select {|i| textlist == "Orange"}
    => [1, 4, 6]

    I have

    irb(main):001:0> textlist = ["Apple", "Orange", "Lemon", "Grape",
    "Orange", "Melon",
    irb(main):002:1* "Orange", "Banana"]
    => ["Apple", "Orange", "Lemon", "Grape", "Orange", "Melon", "Orange", "Banana"]
    irb(main):003:0> textlist.each_with_index.partition {|a,i| a ==
    "Orange"}.first.map {|a,i| i}
    => [1, 4, 6]

    or, even better

    irb(main):006:0> textlist.each_with_index.select {|a,i| a ==
    "Orange"}.map {|a,i| i}
    => [1, 4, 6]

    Hmmm, we could also do

    irb(main):007:0> textlist.each_with_index.select {|a,i| a ==
    "Orange"}.map(&:last)
    => [1, 4, 6]

    This is all very 1.9ish though. ;-)

    Kind regards

    robert


    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
     
    Robert Klemme, Feb 26, 2010
    #10
  11. On Fri, Feb 26, 2010 at 7:13 AM, David Springer <> wrote:
    > after some inspiration from Luc I was able to come up with this:
    >
    > textlist = ["Apple", "Orange", "Lemon", "Grape", "Orange",
    >
    > (0..textlist.length-1).select {|i| textlist == "Orange"}[1]
    >


    Same thing only different :)

    p textlist.fill{|x| x if textlist[x] == "Orange"}.compact[1]


    Harry
     
    Harry Kakueki, Feb 27, 2010
    #11
  12. Here's my solution in 1.9:

    class Array
    def indices_of(value)
    indices = self.each_with_index.select { |v, i| v == value
    }.collect{|v, i| i }
    indices.empty? ? nil : indices
    end
    end
    --
    Posted via http://www.ruby-forum.com/.
     
    Alex Baranosky, Feb 28, 2010
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. karthikeyavenkat
    Replies:
    2
    Views:
    671
    Bryce
    Mar 17, 2005
  2. Shawn W_
    Replies:
    5
    Views:
    319
    Aldric Giacomoni
    Sep 16, 2009
  3. ngoc
    Replies:
    5
    Views:
    199
    Tad McClellan
    May 11, 2006
  4. Tomasz Chmielewski

    sorting index-15, index-9, index-110 "the human way"?

    Tomasz Chmielewski, Mar 4, 2008, in forum: Perl Misc
    Replies:
    4
    Views:
    356
    Tomasz Chmielewski
    Mar 4, 2008
  5. Stefan Mueller
    Replies:
    7
    Views:
    213
    Thomas 'PointedEars' Lahn
    Nov 28, 2005
Loading...

Share This Page