On Tue, Apr 15, 2008 at 12:47:21AM +0900, Vincent A. wrote:
countarray = [3,4,6]
and the expected result should be 0,1,2,5, right?
What gives?
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[x] = 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.