On Apr 14, 12:47 pm, Vincent Angeloni <nospam7... / 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?
>

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,
--
Luis Lavena