2010/2/26 Giampiero Z. [email protected]:
in order to simplify …
(0…textlist.length).select {|i| textlist[i] == “Orange”}[1]
David S. 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