Re: Delete every other value in an array
by Ara.T.Howard other posts by this author
May 9 2008 8:17AM messages near this date
Re: Delete every other value in an array
|
Re: Delete every other value in an array
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
Thread:
Tim Conner
7stud --
S2
David A. Black
7stud --
Joel VanderWerf
Yermej
Robert Dober
Robert Dober
Ara.T.Howard
James Gray
rgs
Ara.T.Howard
Harry Kakueki
Harry Kakueki
Harry Kakueki
Yermej
Ara.T.Howard
|