Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Array index question

0 views
Skip to first unread message

John Smith

unread,
Feb 25, 2010, 11:12:30 AM2/25/10
to
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/.

Rob Biedenharn

unread,
Feb 25, 2010, 12:15:16 PM2/25/10
to
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
R...@AgileConsultingLLC.com


David Springer

unread,
Feb 25, 2010, 1:20:46 PM2/25/10
to
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[i] << "\""
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"

Luc Heinrich

unread,
Feb 25, 2010, 1:39:29 PM2/25/10
to
On 25 févr. 2010, at 19:20, David Springer wrote:

> textlist = ["Apple", "Orange", "Lemon", "Grape", "Orange",
> "Melon","Orange", "Banana"]
>
> i = textlist.index("Orange")
>
> if !i.nil?
> puts "textlist[" << i.to_s << "] is \"" << textlist[i] << "\""
> 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

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

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

p textlist.indexes_of("Orange")

#=> [1,4,6]

--
Luc Heinrich - l...@honk-honk.com


David Springer

unread,
Feb 25, 2010, 5:13:20 PM2/25/10
to
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[i] == "Orange"}[1]

Paul Harrington

unread,
Feb 25, 2010, 5:51:19 PM2/25/10
to
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[i] == "Orange"}[1]

Second day with Ruby, huh...

Marc Heiler

unread,
Feb 25, 2010, 6:35:44 PM2/25/10
to
> Second day with Ruby, huh...

Ruby simplifies thinking.

Sven Schott

unread,
Feb 25, 2010, 7:23:14 PM2/25/10
to
[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. :)

Giampiero Zanchi

unread,
Feb 26, 2010, 6:04:13 AM2/26/10
to
in order to simplify ...
(0...textlist.length).select {|i| textlist[i] == "Orange"}[1]

Robert Klemme

unread,
Feb 26, 2010, 9:07:14 AM2/26/10
to
2010/2/26 Giampiero Zanchi <ci...@tin.it>:

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

Interesting approach. That could also be done with

irb(main):005:0> textlist.size.times.select {|i| textlist[i] == "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/

Harry Kakueki

unread,
Feb 27, 2010, 6:51:17 PM2/27/10
to
On Fri, Feb 26, 2010 at 7:13 AM, David Springer <dnspr...@gmail.com> 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[i] == "Orange"}[1]
>

Same thing only different :)

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


Harry

Alex Baranosky

unread,
Feb 28, 2010, 1:55:50 AM2/28/10
to
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

0 new messages