From: "ara.t.howard" Date: 2008-05-10T00:11:06+09:00 Subject: 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