On Mon, Apr 14, 2008 at 5:47 PM, Vincent Angeloni <nospam7272 / mac.com> wrote:
> Hi and greetings to all group members!
>
>  I'm a new Ruby user with a background in Applescript, Hypertalk
>  (Supercard), and a little Unix. I was intrigued by Matt Neuburg's
>  article about Applescript and Ruby, and that's what got me reading some
>  intro books on Ruby. I just bought Textmate and I am lovin' it!
>
>  Anyway, I am wondering why this seemingly simple script fails:
>
>  sizeList = [0,1,2,3,4,5,6]
>  countarray = [3,4,6]
>  countarray.each {|x| sizeList.delete_at(x)}
>
>  what I want to happen is that sizelist gets deleted at the positions
>  specified in countArray, but the result I am getting is:
>
>  0
>  1
>  2
>  4
>  6
>
>  and the expected result should be 0,1,2,5, right?
>  What gives?

Every time you delete an element, all the values to the right are
shifted one position. What you are doing essentially is:

irb(main):001:0> a = [0,1,2,3,4,5,6]
=> [0, 1, 2, 3, 4, 5, 6]
irb(main):002:0> a.delete_at(3)
=> 3
irb(main):003:0> a
=> [0, 1, 2, 4, 5, 6]
irb(main):004:0> a.delete_at(4)
=> 5
irb(main):005:0> a
=> [0, 1, 2, 4, 6]
irb(main):006:0> a.delete_at(6)
=> nil
irb(main):007:0> a
=> [0, 1, 2, 4, 6]

After the first delete_at, the elements 4,5 and 6 are shifted to fill the space
of the deleted element. So now, the element at index 4 is not the 4, it's the 5.
After those two deletions, the array no longer holds an element with index 6, so
the last deletion does nothing.

Hope this helps,

Jesus.