When you delete_at(3), your new array looks like:
sizeList[0] = 0
sizeList[1] = 1
sizeList[2] = 2
sizeList[3] = 4
sizeList[4] = 5
sizeList[5] = 6
. . . because sizeList[3] = 3.
When you then delete_at(4), your new array looks like:
sizeList[0] = 0
sizeList[1] = 1
sizeList[2] = 2
sizeList[3] = 4
sizeList[4] = 6
. . . because sizeList[4] = 5. Et cetera. Every time you delete an
element out of the middle, you shift everything to the right of it
leftward, basically.
You could try this:
sizeList = [0,1,2,3,4,5,6]
countarray = [6,4,3]
countarray.each {|x| sizeList.delete_at(x)}
That would start at the far end of the array and work backwards, so that
positional shifts after a deleted element won't affect numbering for
stuff you want to delete later. If you can't guarantee that the numbers
in countarray will be in reverse numerical order when you get them, you
can sort them that way thusly:
countarray = [3,6,4].sort.reverse
If you want to keep the array intact, rather than removing elements, and
just reset the values of specific elements to be "nothing", you could
instead do something like this:
countarray.each {|x| sizeList = nil}
There are probably more elegant ways to do what you want -- and I'm not
entirely sure what you're trying to accomplish anyway -- but this might
help get you on the right track.
···
On Tue, Apr 15, 2008 at 12:47:21AM +0900, Vincent Angeloni 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?
--
CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ]
Rudy Giuliani: "You have free speech so I can be heard."