Prev: Error on iterating through a set of nested tables in a form
Next: Making a VALUE visible to multiple source files
From: John Smith on 25 Feb 2010 11:12 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/.
From: Rob Biedenharn on 25 Feb 2010 12:15 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(a)AgileConsultingLLC.com
From: David Springer on 25 Feb 2010 13:20 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/.
From: Luc Heinrich on 25 Feb 2010 13:39 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 - luc(a)honk-honk.com
From: David Springer on 25 Feb 2010 17:13 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/.
|
Next
|
Last
Pages: 1 2 3 Prev: Error on iterating through a set of nested tables in a form Next: Making a VALUE visible to multiple source files |