From: Vincent Angeloni Date: 2008-04-15T01:16:17+09:00 Subject: Re: noob 'why doesn't this work' question Luis Lavena wrote: > On Apr 14, 12:47 pm, Vincent Angeloni wrote: >> countarray = [3,4,6] >> >> and the expected result should be 0,1,2,5, right? >> What gives? >> > > Actually no, when you #delete_at one element from an array, the > indexes of it get altered too: > > You can interpret what is doing #each on countarray: > > irb(main):001:0> sizeList = [0,1,2,3,4,5,6] > => [0, 1, 2, 3, 4, 5, 6] > irb(main):002:0> sizeList.delete_at(3) > => 3 > irb(main):003:0> sizeList.delete_at(4) > => 5 > irb(main):004:0> sizeList.delete_at(6) > => nil > > So, each time you iterate over countarray and remove the indicated > position, sizeList gets updated, and thus, the next items in the > countarray no longer reference your expected positions. > > Errr, sound complicated my explanation, anyone with a better written > one? :-) > > HTH, Ah, yes. I knew it was something simple. I'll just sort countarray so that the highest index value is first, and perhaps that will take care of the problem! Thank you. vince -- Posted via http://www.ruby-forum.com/.