On May 9, 2008, at 8:16 AM, Tim Conner wrote:
> What is the best way to delete every other value in a ruby array?
> e.g.
> %w(a b c d e f g h i j k)
>
> becomes => [a c e g i k]
>
> thanks
> --  
> Posted via http://www.ruby-forum.com/.
>



cfp:~ > cat a.rb
# the list
   a = %w(a b c d e f g h i j k)

# build your index up all at once
   evens = Array.new(a.size / 2){|i| i * 2}
   ods = Array.new(a.size / 2){|i| i * 2 + 1}

# apply it
   p a.values_at(*evens)
   p a.values_at(*ods)

# apply it destructively
   a.replace a.values_at(*evens)
   p a




cfp:~ > ruby a.rb
["a", "c", "e", "g", "i"]
["b", "d", "f", "h", "j"]
["a", "c", "e", "g", "i"]




a @ http://codeforpeople.com/
--
we can deny everything, except that we have the possibility of being  
better. simply reflect on that.
h.h. the 14th dalai lama