Re: Delete every other value in an array
by S2 other posts by this author
May 10 2008 12:20PM messages near this date
Re: Delete every other value in an array
|
Re: Delete every other value in an array
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
irb(main):001:0> a = ("a".."z").to_a
=>
["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "
s", "t", "u", "v", "w", "x", "y", "z"]
irb(main):002:0> for i in 1..a.size/2
irb(main):003:1> a.delete_at(i)
irb(main):004:1> end
=> 1..13
irb(main):005:0> a
=> ["a", "c", "e", "g", "i", "k", "m", "o", "q", "s", "u", "w", "y"]
irb(main):006:0>
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
|