From: yermej Date: 2008-05-11T02:05:06+09:00 Subject: Re: Delete every other value in an array On May 9, 10:08 am, "ara.t.howard" wrote: > On May 9, 2008, at 8:35 AM, yermej wrote: > > > a = %w(a b c d e f g h i j k) > > 1.upto(a.size) {|i| a.delete_at i} > > it does, but accidentally: > > cfp:~ > cat a.rb > > a = %w(a b c d e f g h i j k) > > 1.upto(a.size) do |i| > puts '---' > p :i => i > p :before => a > a.delete_at i > p :after => a > end > > cfp:~ > ruby a.rb > --- > {:i=>1} > {:before=>["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]} > {:after=>["a", "c", "d", "e", "f", "g", "h", "i", "j", "k"]} > --- > {:i=>2} > {:before=>["a", "c", "d", "e", "f", "g", "h", "i", "j", "k"]} > {:after=>["a", "c", "e", "f", "g", "h", "i", "j", "k"]} > --- > {:i=>3} > {:before=>["a", "c", "e", "f", "g", "h", "i", "j", "k"]} > {:after=>["a", "c", "e", "g", "h", "i", "j", "k"]} > --- > {:i=>4} > {:before=>["a", "c", "e", "g", "h", "i", "j", "k"]} > {:after=>["a", "c", "e", "g", "i", "j", "k"]} > --- > {:i=>5} > {:before=>["a", "c", "e", "g", "i", "j", "k"]} > {:after=>["a", "c", "e", "g", "i", "k"]} > --- > {:i=>6} > {:before=>["a", "c", "e", "g", "i", "k"]} > {:after=>["a", "c", "e", "g", "i", "k"]} > --- > {:i=>7} > {:before=>["a", "c", "e", "g", "i", "k"]} > {:after=>["a", "c", "e", "g", "i", "k"]} > --- > {:i=>8} > {:before=>["a", "c", "e", "g", "i", "k"]} > {:after=>["a", "c", "e", "g", "i", "k"]} > --- > {:i=>9} > {:before=>["a", "c", "e", "g", "i", "k"]} > {:after=>["a", "c", "e", "g", "i", "k"]} > --- > {:i=>10} > {:before=>["a", "c", "e", "g", "i", "k"]} > {:after=>["a", "c", "e", "g", "i", "k"]} > --- > {:i=>11} > {:before=>["a", "c", "e", "g", "i", "k"]} > {:after=>["a", "c", "e", "g", "i", "k"]} > > look carefully at what's happening for i >= 6. > > the indexes map only by accident since each delete modifies the > mapping in the array (size reduced by one after each delete) > > 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 Oops...I really should have noticed that. In a hurry, I guess. I'll vote for: a = %w(a b c d e f g h i j k) 1.upto(a.size/2) {|i| a.delete_at i} as the accidentally obfuscated solution of the day.