Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Array index question

Reply
Thread Tools

Array index question

 
 
John Smith
Guest
Posts: n/a
 
      02-25-2010
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/.

 
Reply With Quote
 
 
 
 
Rob Biedenharn
Guest
Posts: n/a
 
      02-25-2010
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
http://www.velocityreviews.com/forums/(E-Mail Removed)




 
Reply With Quote
 
 
 
 
David Springer
Guest
Posts: n/a
 
      02-25-2010
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"
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Luc Heinrich
Guest
Posts: n/a
 
      02-25-2010
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[i] << "\""
> 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 - (E-Mail Removed)


 
Reply With Quote
 
David Springer
Guest
Posts: n/a
 
      02-25-2010
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]



--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Paul Harrington
Guest
Posts: n/a
 
      02-25-2010
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...
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Marc Heiler
Guest
Posts: n/a
 
      02-25-2010
> Second day with Ruby, huh...

Ruby simplifies thinking.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Sven Schott
Guest
Posts: n/a
 
      02-26-2010
[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 <(E-Mail Removed)>wrote:

> > Second day with Ruby, huh...

>
> Ruby simplifies thinking.
> --
> Posted via http://www.ruby-forum.com/.
>
>


 
Reply With Quote
 
Giampiero Zanchi
Guest
Posts: n/a
 
      02-26-2010
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]


--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      02-26-2010
2010/2/26 Giampiero Zanchi <(E-Mail Removed)>:
> 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/

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index" camelean@shaw.ca ASP .Net 3 02-22-2011 07:06 PM
Making an array wrap, where last index + 1 = first index Shawn W_ Ruby 5 09-16-2009 02:45 PM
Templated array of templates specialized by array index npankey@gmail.com C++ 6 10-12-2008 03:20 PM
sorting index-15, index-9, index-110 "the human way"? Tomasz Chmielewski Perl Misc 4 03-04-2008 05:01 PM
problem with index.html .(page is automatically gettin redirected to index.html) karthikeyavenkat Java 2 03-17-2005 10:01 PM



Advertisments