Re: Delete every other value in an array
by Ara.T.Howard other posts by this author
May 9 2008 8:11AM 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:37 AM, Raúl Gutiérrez S. wrote:
> v = %w(a b c d e f g h i j k)
>
> cnt = 0
> v.each { |i|
> v.delete(0) if cnt % 2 != 0
> cnt += 1
> }
are you sure? ;-)
cfp:~ > cat a.rb
v = %w(a b c d e f g h i j k)
cnt = 0
v.each do |i|
puts '---'
p :i => i
p :before => v
v.delete(0) if cnt % 2 != 0
cnt += 1
p :after => v
end
puts '==='
p v
cfp:~ > ruby a.rb
---
{:i=> "a"}
{:before=> ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]}
{:after=> ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]}
---
{:i=> "b"}
{:before=> ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]}
{:after=> ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]}
---
{:i=> "c"}
{:before=> ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]}
{:after=> ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]}
---
{:i=> "d"}
{:before=> ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]}
{:after=> ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]}
---
{:i=> "e"}
{:before=> ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]}
{:after=> ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]}
---
{:i=> "f"}
{:before=> ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]}
{:after=> ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]}
---
{:i=> "g"}
{:before=> ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]}
{:after=> ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]}
---
{:i=> "h"}
{:before=> ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]}
{:after=> ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]}
---
{:i=> "i"}
{:before=> ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]}
{:after=> ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]}
---
{:i=> "j"}
{:before=> ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]}
{:after=> ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]}
---
{:i=> "k"}
{:before=> ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]}
{:after=> ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]}
===
["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]
you cannot simultaneously iterate and delete from and enumerable in
ruby.
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
|